๐Ÿ—ƒ๏ธWorking with Storage Files

How to prompt while working with Databutton's inbuilt storage

The in-built Databutton storage supports various file types, such as text, CSV, and binary files. This storage is very useful for testing purposes, such as reading or writing files and testing endpoints.

We recommend using this storage for testing or small-scale applications. For scalable, production-level apps, consider alternatives like Firebase or Supabase. We have a mini tutorial on how to integrate Firebase.

How to upload files

  1. Go to the Storage section ( left hand side of the workspace )

  2. Click "Add File."

  3. Upload / Drag & Drop the desired file from your computer

What should be a typical workflow while working with storage files ?

  1. Fetch using Databutton's SDK: db.storage.binary.get() or db.storage.text.get()

  2. Read or perform operations using external Python libraries

Can Databutton guide me to perform this workflow?

Yes!

How to prompt your files

Databutton allows you to reference your files easily using the hashtag (#) symbol.

Selecting a specific file with an additional prompt enables Databutton to work its magic.

Text Files

#untitled-txt how to read this file

Databutton uses its own Python SDK to read files. It figures out the file type and necessary methods to assist users in building the endpoint efficiently.

Once the file is retrieved from storage, the downstream process can be managed smoothly based on the backend's final goal.

PDF / Binary Files

Here's an example of Databutton handling a PDF file using the db.storage.binary.get() method. Next, Databutton uses an external Python library (PyMuPDF) to read the content of the PDF.

@router.get("/extract", response_model=ExtractResponse)
def extract_text(filename: str) -> ExtractResponse:
    print(f"Received text extraction request for filename: {filename}")
    # Fetch the file from storage
    try:
        file_content = db.storage.binary.get(filename)
        print(f"Fetched file from storage: {filename}")
    except FileNotFoundError:
        print(f"File not found: {filename}")
        raise HTTPException(status_code=404, detail="File not found.")

    # Extract text from the PDF
    pdf_document = fitz.open(stream=file_content, filetype="pdf")
    text = ""
    for page_num in range(pdf_document.page_count):
        page = pdf_document.load_page(page_num)
        text += page.get_text()
    print(f"Extracted text from file: {filename}")

    return ExtractResponse(text=text)

You will find the entire use case here.

Note: Soon, we will support working with image files. For the time being, follow the workaround here.

Databutton inbuilt storage also works fine with binary files like .mp3 or wav. You can find working with such files here.

Prompting tips to keep in mind ?

  • Using hastags to refer specific files

  • Prompting Databutton to do what after the file is fetched.

Last updated