Streamlit restful app

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: https://github.com/streamlit/streamlit/issues/439


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

    st.write('''
        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
        app.
    ''')

    from flask import Flask

    app = Flask(__name__)

    @app.route('/foo')
    def serve_foo():
        return 'This page is served via Flask!'

    app.run(port=8888)


# 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.

7 Likes