Shortcuts

StreamlitFrontend

class lightning_app.frontend.stream_lit.StreamlitFrontend(render_fn)[source]

Bases: lightning_app.frontend.frontend.Frontend

A frontend for wrapping Streamlit code in your LightingFlow.

Return this in your LightningFlow.configure_layout() method if you wish to build the UI with streamlit. To use this frontend, you must first install the streamlit package (if running locally):

pip install streamlit
Parameters

render_fn (Callable) – A function that contains your streamlit code. This function must accept exactly one argument, the AppState object which you can use to access variables in your flow (see example below).

Example

In your LightningFlow, override the method configure_layout:

class MyFlow(LightningFlow):
    def __init__(self):
        super().__init__()
        self.counter = 0

    def configure_layout(self):
        return StreamlitFrontend(render_fn=my_streamlit_ui)


# define this function anywhere you want
# this gets called anytime the UI needs to refresh
def my_streamlit_ui(state):
    import streamlit as st

    st.write("Hello from streamlit!")
    st.write(state.counter)
start_server(host, port)[source]

Start the process that serves the UI at the given hostname and port number.

Parameters
  • host (str) – The hostname where the UI will be served. This gets determined by the dispatcher (e.g., cloud), but defaults to localhost when running locally.

  • port (int) – The port number where the UI will be served. This gets determined by the dispatcher, which by default chooses any free port when running locally.

  • root_path – root_path for the server if app in exposed via a proxy at /<root_path>

Example

An custom implementation could look like this:

def start_server(self, host, port, root_path=""):
    self._process = subprocess.Popen(["flask", "run" "--host", host, "--port", str(port)])
Return type

None

stop_server()[source]

Stop the process that was started with start_server() so the App can shut down.

This method gets called when the LightningApp terminates.

Example

def stop_server(self):
    self._process.kill()
Return type

None