Hey Streamlit Community - I am attempting to use the download_button component, but not quite sure how the “data” accepts a “file” value. I see from the docs that a file can be submitted as the data, and I have a file that was generated and available in the same directory.
Any thoughts on what I may be missing or how to approach this?
with open('data.jsonl', 'w') as jsonfile:
for line in data_list:
jsonfile.write(json.dumps(line)+'\n')
st.download_button(label="Download JSON", data=jsonfile, file_name='data.jsonl', mime='application/json')
Additionally, I’ve tried to 1) include a “path” to the file 2) put the path as a string, and 3) insert the jsonlife variable as shown above as the data source. Any guidance would be appreciated. Thanks
Since you’re using a text-based file type, you can just pass the data directly as a string, like this:
import streamlit as st
data = [
{"name": "John", "age": 20},
{"name": "Jane", "age": 21},
{"name": "Jack", "age": 22},
]
# Add button to download as jsonl
st.download_button(
"Download as jsonl",
data="\n".join([str(d) for d in data]),
file_name="output.jsonl",
mime="application/jsonl",
)
Alternatively, if you already have it in a file, you can just read the file and pass that as the data.
from pathlib import Path
st.download_button(
"Download from file",
data=Path("output.jsonl").read_text(),
file_name="output.jsonl",
mime="application/jsonl",
)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.