Databutton includes built-in storage compatible with various popular file formats, such as text files, CSV files, JSON files, binary files, and even pandas DataFrames.
Databutton Agent can read and write files using the Databutton Python SDK.
Databutton storage is suitable for testing and small-scale production apps. For larger file sizes and scaling up, it's advisable to use external databases like Firebase or Supabase.
Alternatively, manually any files can be uploaded as well,
Navigate to the "Storage" tab on the Databutton platform.
Click on "Upload my data" to initiate the upload process.
Select the data file you wish to upload from your device.
Examples using the Databutton's Storage endpoint.
Creating a new dataframe
To create a new or replace an existing dataframe use the following API from any view or job:
import databutton as dbimport pandas as pd# This can be data scraped from API, input from a form,# or anything else that fits in a dataframe.data_to_store = []df = pd.DataFrame(data=data_to_store)# Save the dataframe 'df' to the key 'my-dataframe',# making it accessible in your views, jobs, and web UIdb.storage.dataframes.put("my-dataframe", df)
Reading from a dataframe
To access a dataframes in a job or view, Databutton provides the following API:
import databutton as db# Returns the dataframe stored under 'my-dataframe' or# an empty dataframe if it does not exist.df = db.storage.dataframes.get(key="my-dataframe")
By using db.storage.dataframes.get(key), you can access your dataframes anywhere.
Deleting a dataframe
import databutton as db# deletes the dataframe with the given keydf = db.storage.dataframes.delete(key="my-dataframe")
This will delete the whole dataframe, also its entry in the Data Storage.
Adding records to a dataframe
In order to add new records to a dataframe, e.g. in a scraping job, you could do the following:
import databutton as dbimport pandas as pd# Fetch existing dataframe 'my-dataframe', or if it does# not exist call the 'default' argument to produce the default value.# Here we default to returning an empty dataframe.existing_df = db.storage.dataframes.get( key="my-dataframe", default=lambda: pd.DataFrame())# Data to add to the dataframe. Could be scraped from an API of your choice.data = [{"x":10,"y":5}]new_df = pd.DataFrame(data)# Concatinate the two dataframesdf_to_store = pd.concat([existing_df, new_df], ignore_index=True)# Store the dataframe safely in Databuttondb.storage.dataframes.put(key="my-dataframe", value=df_to_store)
Working with json files
Use json files to work with nested dict-like structures for smaller and unstructured data. An example use case is taking basic form data input in a view:
import databutton as dbimport streamlit as stconfig = db.storage.json.get("config", default=lambda: {"email":"default@example.com"})# Use streamlit formwith st.form("my-form"): email = st.text_input("Email", placeholder=config.get("email", ""))if st.form_submit_button("Submit"):# Define a dict with configuration to store as json config ={"email": email}# Replace existing config db.storage.json.put("config", config)# Show in view that config has been safely stored st.write("Notification email has been changed!")
Working with text files
Text files are simply fetched as Python strings. If you need to pass it to a library expecting a text file you can wrap the str in io.TextIO like this:
import databutton as db# Get some html page from somewherescraped_html ="""Some <b>very</b> interesting content."""# Store it as text in your data appdb.storage.text.put("webpage.html", scraped_html)# Later retrieve it from databutton storagehtml = db.storage.text.get("webpage.html")
Working with pickle
Pickle files works out of the box with db.storage.binary, here's an example
A file of a type unknown to Databutton can still be stored as a binary file, and fetched as a Python bytes object. If you need to pass it to a library expecting a file as input or output you can wrap the bytes in io.BytesIO like in this basic image processing example using pillow:
import ioimport databutton as dbimport requestsfrom PIL import Image# Get image from URLurl ="http://images.cocodataset.org/val2017/000000039769.jpg"image = Image.open(requests.get(url, stream=True).raw)# Apply some image processingimage = image.rotate(45)# Save image to in-memory bytes bufferimage_buffer = io.BytesIO()image.save(image_buffer, format="jpeg")# Put it to databutton storageimage_buffer.seek(0)db.storage.binary.put("img.jpeg", image_buffer.read())# Read it back from databutton storageimage_bytes = db.storage.binary.get("img.jpeg")image = Image.open(io.BytesIO(image_bytes))