How to share custom files through my Streamlit app?

Hello guys,

my goals is to share custom files through my Streamlit app such as jupyter notebook file (.ipynb) or GeoJson (.geojson).

I know Streamlit provide a download button which I can use to share file. Let’s me share with you an example:

import streamlit as st
import nbformat as nbf

# build my notebook 
notebook = nbf.v4.new_notebook()

# enriched my notebook
notebook["cells"] = [nbf.v4.new_markdown_cell("Blablabla"), ...]

# create my download button
st.download_button("Download the notebook", notebook, mime="application/vnd.jupyter")

This is not working !
I am not sure if I made a mistake in my choice for the parameters “mime” or I need to format the notebook to an other format

You can overpass the problem by formating the notebook in an html file :

html_exporter = HTMLExporter()
html_data, _ = html_exporter.from_notebook_node(notebook)

st.download_button("Download the notebook", notebook, mime="text/html")

But I feel more confortable with a notebook file !

Any idea guys ? (:

Hi @axelearning, welcome to the Streamlit community!

I would try application/octet-stream as the mime-type, which is the generic type for any sort of bytes.


Hi @randyzwitch thanks for your fast answer :zap:

I try to do it :

nbf.write(notebook, "notebook.ipynb")
with open("notebook.ipynb", "rb") as nb_file:
            st.download_button("Jupyter notebook", nb_file, mime="application/octet-stream")

But the output of the downloaded file is weird I can’t open it :frowning:

I find a way to do it using a zipfile but this would be wonderfull without it :stuck_out_tongue:

nbf.write(notebook, "notebook.ipynb")

# create a ZipFile object
zipObj = ZipFile("", "w")

with open("", "rb") as zipfile:
     st.download_button("Jupyter notebook", zipfile, mime="application/zip")```

It sounds like you’re on the right track, changing the mime type. If it doesn’t work as an octet-stream, I’d try application/json, as notebooks are fundamentally a JSON document.

Good luck!