Uploading, downloading files and equations in text

Hi. I discovered streamlit last week and enjoyed working with so much that I ended up writing a small app for fitting experimental data.

However, there are a few things that the app is missing before I can make it available to everyone:

  • First thing is a file uploader from the client to the server (so the users can upload their own data that the app then reads). I believe this is work in progress (https://github.com/streamlit/streamlit/issues/120) so I just need to be patient.
  • I want the user to be able to download the output data from the fitting. I’ve managed to store the data in a file on the Heroku server where my app is working on, however, I’m not sure how to create a download link for the file.
  • Last thing, it would be great if we could display simple math equations, not only in streamlit.text but also streamlit.selectbox although that is not very important for now in my case.

Thanks a lot for your help and suggestions.

3 Likes

Definitely agree here! Right now I’m having the users enter the email address, and then receive the data via email (or worse, copy and paste the dataframe if it’s small enough).

It would be so great if the users could simply download the data directly from the browser!

Hello, @Emmanuel_Pean! Welcome to Streamlit, and thanks for your suggestions!

  1. Yeah - as you noted, we’re working on a File Uploader Widget that will be released in a couple of weeks. (This has been a very frequent feature request, so we’re prioritizing it.)
  2. Streamlit does not have a way to serve file downloads. We’re tracking that request at this Github issue - please feel free to thumbs-up the issue if it’s important to you; we take user demand into consideration when prioritizing new feature development!
  3. LaTeX support was just merged into Streamlit! This will go out in a Streamlit release in a couple weeks as well. (You can also use the Streamlit develop branch directly if you want to use features that have been finished but not yet released to PyPI.)

As a workaround for file download support, here’s a Gist that demonstrates patching Streamlit to allow static file downloads: https://gist.github.com/tconkling/1e5ead87c796a82de7fa71fcc4a74777. (And of course, it probably goes without saying, but you want to be very careful about what resources you expose to the internet!)

Cheers!

4 Likes

Hi @tim,

Great, I look forward to see (1) and (3) implemented in streamlit!
Thanks for the link to the request for downloading files. I’ve modified the side-packages/streamlit/server/Server.py file using the gist you provided, however, when I try to run the app locally, it just doesn’t display anything anymore (the page is completely blank).
Any idea what is the problem?

Cheers,

Emmanuel

Whoops, looks like I forgot to note that you also need to add import os to Server.py, before the TORNADO_SETTINGS dict. I’ve updated the gist - can you give that a try?

If it doesn’t work, can you paste any console output that you get from streamlit run ...? I’ve tested this locally, but not extensively across different OSs and Python versions.

1 Like

I’m not getting any error message when running the app locally. I did a few tests and here’s what I found: It works after I add "static_path": os.path.join(os.getcwd(), "static") to the dict. After a few minutes, if I reload the app, it just load a blank page. If I comment "static_path": os.path.join(os.getcwd(), "static") and re-run the app locally, it works again.

UPDATE: It seems to working fine now locally (no idea why how it fixed itself though). However, I can’t manage to download the test file I put in the static folder after uploading the app on Heroku. I use https://appname.herokuapp.com:80/static/testfile and I get a ERR_SSL_PROTOCOL_ERROR error message after a few seconds.

I can confirm that changing that line in the Server.py file causes Streamlit to just load a blank page, and commenting it out causes it work fine again. I was working in Python3.7.4 on Catalina 10.15.

Hey @jamestjw - just making sure you saw that there are two lines that need changing: a line to add the config option to TORNADO_SETTINGS, and also a line to import os.

(If you’re still seeing a blank page after that, could you paste the Streamlit output from your terminal?)

Well, that’s just it, there is no visible error message or anything. I’m a little unsure what’s going on here, but as soon as I comment out that line in the TORNADO_SETTINGS, things start working again. And yes I also imported os.

Quick update: Blank page situation happens on safari, but not in Chrome. However, when I use the network URL instead of the local URL, the problem occurs on all browsers.

1 Like

I am trying out this as well, and I am finding the same problem. I am able to download a static file, but the page just stays blank. By looking at the network tab it seems that all assets that were being fetched on static originally returns 404. Likely, the new setting is conflicting.

I am going to do some debug on this and see what I can find…

Best,

Matteo

This is what I came up with:

This gist is a patch to Server.py that adds an endpoint called text. This endpoint points at a static file in a local directory called custom_static.

Of course, this is kind of a short term solution…
The github issue seems to have other proposals to do this as well.

Matteo

2 Likes

The app seems to work well locally and on Heroku with this patch however, I still can’t download the test file using https://appname.herokuapp.com/:8501/custom_static/text. Is the link I’m using wrong?

@Emmanuel_Pean, with the patch I gave you, the link would be https://appname.herokuapp.com/:8501/text

Unsure your address is correct, but what I mean, you don’t have to add custom_static. You can change this behavior by changing line 26 of my patch. You need to change the matching regex that is the first element of the tuple on that line.

Matteo

Hi @monchier, thanks for your reply.
It’s working locally but not on Heroku (here’s the link to my app https://trpl-fit-tool.herokuapp.com/).

Emmanuel

Interesting, I am not a Heroku expert, but here is what I can see from quickly looking at the app you published:
This link http://localhost:8501/text will not work because it is referring to localhost which is your local machine.
About your second link. There might be a problem is with /:8501. You are publishing your app on port 80, so no need to give it the Streamlit port number. Heroku will transalte that for you. Also, you should not be using the / before the port number in any case. I would say this should work: http://trpl-fit-tool.herokuapp.com/text, but when I try it in my browser it does not. There might be an extra configuration step on the Heroku side that is needed. Can you also double check you have deployed the text file in your Heroku container and that is in the right directory?

Note that I also haven’t used https here. Not sure how you have configured that, but I am not sure there is some extra step in configuring Heroku to get it to work with https. You can try both version in any case.

Best
Matteo

According to the Heroku docs, https is activated by default so you can access the app with https://trpl-fit-tool.herokuapp.com/. The file is definitely on the server, however, I have no idea how to configurate the server so that the file can be dowloaded.

Emmanuel

Hi @Emmanuel_Pean, unfortunately that’s about the extent of my knowledge of Heroku, but if anyone in the community wants to weigh in we’d love that. Also, it won’t be ready until early next year but deploying apps through Streamlit is going to be a component of our For Teams offering. If you’d like to sign up for more information here is a link: https://streamlit.io/forteams/

Best,
Matteo

1 Like

Hi All

I’ve discovered a workaround for downloading small files from streamlit.

I’ve added it to the gallery at awesome-streamlit.org. It’s called “File Download Workaround”. You can try it out and copy the code.

2 Likes

Kudos! It works perfectly!
May I ask what is the maximum file size or string length that can be downloaded with this method?

2 Likes

I would not know. At first I thought it would be small. But I can see this site https://www.base64encode.org/enc/length/ has a limit of 192MB!

According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs Firefox supports unlimited length in theory.

2 Likes