Where is the PDF output from my python script stored using fpdf?

Options
DataAnalyst24
DataAnalyst24 Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Registered Posts: 7

I installed the fpdf python package in a code env.

I created a python notebook and I run this code

import fpdf
from fpdf import FPDF

# Create instance of FPDF class
pdf = FPDF()

# Add a page
pdf.add_page()

# Set font
pdf.set_font("Arial", size = 15)

# Add a cell
pdf.cell(200, 10, txt = "Hello", ln = True, align = 'C')

# Save the pdf with name .pdf
pdf.output("report.pdf")

Where does report.pdf go?

Best Answer

  • Alexandru
    Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,209 Dataiker
    edited July 17 Answer ✓
    Options

    Hi @DataAnalyst24
    ,

    By default, it will be the current working directory of the notebook e.g cwd
    You can read it from the notebook using the relative path "report.pdf".

    What you probably need to do is simply store this in a managed folder instead.
    You can use upload_file for example:

    folder_id = "FOLDER_ID"
    folder = dataiku.Folder(folder_id)
    # Get the current working directory
    cwd = os.getcwd()
    # Specify the filename of the PDF file
    pdf_filename = "report.pdf"
    local_file_path = os.path.join(cwd, pdf_filename)
    destination_filename = "uploaded_report.pdf"
    
    #upload the file to the Dataiku managed folder
    folder.upload_file(destination_filename, local_file_path)

Answers

  • DataAnalyst24
    DataAnalyst24 Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Registered Posts: 7
    edited July 17
    Options

    When I run

    cwd = os.getcwd()

    I get this error

      11 # folder.upload_file(destination_filename, local_file_path)
    ---> 12 cwd = os.getcwd()
         13 display(cwd)
    
    NameError: name 'os' is not defined

    How do I define os?

  • Alexandru
    Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,209 Dataiker
    Options

    Hi,

    You will need to add

    import os

    import dataiku

    At the top of the script.

  • DataAnalyst24
    DataAnalyst24 Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Registered Posts: 7
    Options

    Thank you @AlexT
    !

    Your solution works. I can see the PDF in the folder now.

    Do you also know other alternatives where I can build a PDF and immediately see a preview of the PDF document I am creating?

Setup Info
    Tags
      Help me…