Serve streamlit within flask

Hi,
Thank you from streamlit. It’s really an amazing product.
We use flask internally to build a variety of analytical apps.
Would it be possible to serve a streamlit app within flask?

Thank you

Hey @Alvaro_Ferrer, thanks for checking out Streamlit, and welcome to the community!

Internally, Streamlit uses Tornado to serve HTTP and WebSocket data to its frontend. That is, it’s already its own web server, and is written in an existing web framework; it wouldn’t be trivial to wrap it inside another web framework.

(It’s probably possible to refactor Streamlit to use Flask instead of Tornado, but perhaps not worth the trouble?)

Can you give some more detail about what you’re trying to accomplish?

I can see a use case for this: a webapp that serves many streamlit apps. This would help automate deployment off the apps. Create a folder in a repo, push your code and CI handles the rest, you now have a indexed webapp served through flask instead of many streamlit apps spread out.

Hi Tim,
I’m no hardcore developer so I wouldn’t fully understand the implications of this so excuse me before hand if making an unreasonable point here.

I oversee data and analytics operations at my co., and a big issue is trying to unify platforms for the different teams for analysis and reporting purposes.
As such, we are committed to a proprietary platform built on Flask that we use to report on data that is usually not a fit for BI tools. Honestly, anything that resides in a python notebook is troublesome to be made available and interactive - and i think that is one of the things that makes streamlit especial.

You’re focusing this product as an ML/AI toolkit, but honestly I see how it could be tailored to a much broader audience. We could build data that is easily digestible by operations, customer service and more.
In our specific case, if we could deploy streamlit as part of this bigger reporting framework we would put it to good use right away. Otherwise, it’s an additional platform you’ve got to get people to remember.

Anyway, food for thought. We’re using streamlit currently in the data team but wish it was more easily exportable.

Many thanks again

1 Like

Hi @Alvaro_Ferrer

Here are my few cents.

  • Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.

Flask is a synchronous web framework and not ideal for websockets etc.

Instead you could sign up for https://streamlit.io/forteams/ and consider if you really would be developing Flask applications in the future.

An alternative is to develop a small streamlit app to serve these like the Gallery of the Awesome Streamlit Application at https://github.com/marcskovmadsen/awesome-streamlit

Hi Mark. appreciate your feedback. i had already taken a look at this resource and it’s pretty awesome.

1 Like

Hi,

Is it possible to serve Streamlit app on a VPS server running Apache WSGI. I have some Flask app currently running on this server.

I could get it running using command line and going to the IP address on a web browser. But of course this would close when the command line interface is closed.

Thanks in advance.

Hi @vhphan, welcome to Streamlit!

Streamlit uses the Tornado framework under the hood, which does not support being run in a WSGI container:

The reverse is not supported; the Tornado Application and RequestHandler classes are designed for use with the Tornado HTTPServer and cannot be used in a generic WSGI container.

But Apache can also function as a reverse proxy, using the mod_proxy module. I’m not well-versed in Apache, but I believe you should be able to configure it to re-route your Streamlit requests to your running streamlit app via mod_proxy.

If a user clicked on a check box that produced a graph on streamlit, how can I get the streamlit application to go into the flask-api folder(contact the api) and get/pull the database ??

This is a solution for that:

Main Folder
   Flask-API Folder
      - main.py
      - requirements.txt
      - table1.db
   Streamlit application Folder
      - app.py
      - Dockerfile
      - more python files
      - requirements.txt

In my app.py use the request package.

#app.py
import streamlit as st
import request
import pandas as pd

# http://127.0.01:5000/ is from the flask api
response = request.get("http://127.0.01:5000/")
print(response.json())
data_table1 = pd.DataFrame(response.json())
st.write(data_table1)

What about a compromise? Could you use a docker-compose service to accomplish this? Spitballing here:

Let’s say I dockerized a flask app in compose.
One of the containers serves a GUI, that guides a user to do a thing to some data. You get back a tsv that is stored in a volume.
Then, the user can click a button in that GUI - one that, when clicked, opens a streamlit app running in a separate container. That container loads the data from the volume.

And maybe … it would be possible to have that opened window be embedded in the original window?

AWSGI websocket