forked from asehmi/simple-streamlit-fastapi-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlrp_app.py
86 lines (63 loc) · 2.52 KB
/
lrp_app.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import time
import streamlit as st
import requests
# --------------------------------------------------------------------------------
API_HOST='localhost'
API_PORT=8000
API_BASE_URL='http://localhost:8000'
# from _utils import SessionState
# import SessionState
# Session State variables:
# state = SessionState.get(
# API_APP = None,
# API_STARTED=False,
# )
if 'key' not in st.session_state:
st.session_state['API_APP'] = None
if 'key' not in st.session_state:
st.session_state['API_STARTED'] = False
# --------------------------------------------------------------------------------
# NOTE: Design point... only main() is allowed to mutate state. All supporting functions should not mutate state.
def main():
st.title('LR Process Manager')
# RUN LRP
if not st.session_state['API_STARTED']:
st.write('To launch your LRP click the button below.')
if st.button('\U0001F680 Launch'):
import subprocess
import threading
def run(job):
print (f"\nRunning job: {job}\n")
proc = subprocess.Popen(job)
proc.wait()
return proc
job = ['python', os.path.join('./', 'lrp_bootstrapper.py'), API_HOST, str(API_PORT)]
# server thread will remain active as long as streamlit thread is running, or is manually shutdown
thread = threading.Thread(name='FastAPI-LRP-Bootstrapper', target=run, args=(job,), daemon=True)
thread.start()
time.sleep(2)
# !! Start the LRP !!
requests.get(f'{API_BASE_URL}/run')
st.session_state['API_STARTED'] = True
st.experimental_rerun()
if st.session_state['API_STARTED']:
st.markdown(f'''
The LRP API is running. If you\'d like to terminate the LRP click the button below.
### API docs
- [**http://{API_HOST}:{API_PORT}/docs**](http://{API_HOST}:{API_PORT}/docs)
- [**http://{API_HOST}:{API_PORT}/redoc**](http://{API_HOST}:{API_PORT}/redoc)
''')
if st.button('\U0001F525 Shutdown LRP'):
requests.get(f'{API_BASE_URL}/shutdown')
st.session_state['API_STARTED'] = False
st.experimental_rerun()
def sidebar():
# ABOUT
st.sidebar.header('About')
st.sidebar.info('FastAPI Wrapper to run and stop a LRP!\n\n' + \
'(c) 2021. Oxford Economics Ltd. All rights reserved.')
st.sidebar.markdown('---')
if __name__ == '__main__':
main()
sidebar()