-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
streamlit_base.py
39 lines (28 loc) · 1.25 KB
/
streamlit_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""This file gets run by streamlit, which we launch within Lightning.
From here, we will call the render function that the user provided in ``configure_layout``.
"""
import asyncio
import os
import pydoc
from typing import Callable
from lightning_app.frontend.utils import _reduce_to_flow_scope
from lightning_app.utilities.app_helpers import StreamLitStatePlugin
from lightning_app.utilities.state import AppState
def _get_render_fn_from_environment() -> Callable:
render_fn_name = os.environ["LIGHTNING_RENDER_FUNCTION"]
render_fn_module_file = os.environ["LIGHTNING_RENDER_MODULE_FILE"]
module = pydoc.importfile(render_fn_module_file)
return getattr(module, render_fn_name)
def _main():
"""Run the render_fn with the current flow_state."""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
app_state = AppState(plugin=StreamLitStatePlugin())
# Fetch the information of which flow attaches to this streamlit instance
flow_state = _reduce_to_flow_scope(app_state, flow=os.environ["LIGHTNING_FLOW_NAME"])
# Call the provided render function.
# Pass it the state, scoped to the current flow.
render_fn = _get_render_fn_from_environment()
render_fn(flow_state)
if __name__ == "__main__":
_main()