Streamlit restful app

Is there a way to create a restful app using only streamlit, like Flask?
Or can you provide such an example using Flask? This would be very helpful.
I am loving Streamlit so far.

Hi @Nelson_Silva

That’s an awesome idea. Streamlit doesn’t support that use case at the moment, and after looking at the internals of our code I don’t see a good way to hack it together either.

So I opened a feature request here:

I said I don’t see a good way to hack it, because a nasty hack would be to hijack one of Streamlit’s “script threads” to serve your Flask app. I have no idea how well that would perform, though. Use at your own risk!

import streamlit as st

if not hasattr(st, 'already_started_server'):
    # Hack the fact that Python modules (like st) only load once to
    # keep track of whether this file already ran.
    st.already_started_server = True

        The first time this script executes it will run forever because it's
        running a Flask server.

        Just close this browser tab and open a new one to see your Streamlit

    from flask import Flask

    app = Flask(__name__)

    def serve_foo():
        return 'This page is served via Flask!'

# We'll never reach this part of the code the first time this file executes!

# Your normal Streamlit app goes here:
x = st.slider('Pick a number')
st.write('You picked:', x)

If you run the code above, you can then go to localhost:8888/foo to see some data served via Flask.


I like this workaround, I should try this on my existing app, I like dual apps like this - is there any technical downside to this from REST point of view or websockets or serving point of view?

1 Like

Thanks @thiago I will try this :slight_smile:

@neomatrix369 besides some code style complexity, I think the main downside is just performance. Both servers are running on the same Python process, which means they share the same Global Interpreter Lock.

@Nelson_Silva Please let us know if that worked or not for you :slight_smile:

As streamlit is running on Tornado my guess there would be some Tornado way of offering rest api endpoints.

I would really like this functionality in order to be able to serve csv, excel, parquet etc. files for my users.


This loads flask in 8888 and streamlit in 8501. can then run in same port with different endpoints ?

My guess is that’s not really possible. I don’t think Flask and Tornado (which Streamlit uses) were written with that kind of use case in mind.

That said, we’re actually designing some solutions for the use case @Marc mentioned above, where the developer wants to serve files. So stay tuned!

1 Like

I kinda achieved what i want with hosting in different ports but using F5 to show it as if they are in different endpoints of a single domain

I hope that it can be done.

If it’s possible, I think streamlit is really l33t. I’m enjoying deploying my models in AWS without the hassle of flask and I can edit the codes at VSCODE and view the output once I ctrl-S :slight_smile:

Thank you!

1 Like

I’d love to see that too! It’d be even better if your REST API could be auto generated like @thiago suggested, for instance from your widgets and variables.

Hi. Is there any chance of seeing this functionality?
I believe not having to maintain two separate servers for API and demo would be of very significant value, no?

How does streamlit accomplish the app widgets without building some sort of internet facing API from the functions in the notebook?