-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.py
44 lines (35 loc) · 1.55 KB
/
routing.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
import streamlit as st
from typing import Callable
# Works like flask's app.route. There is currently always only one context(views are shared across all instances)
class Routing_Context:
SESSION_KEY_PATH = "routing_path"
views = {}
def __init__(self, default=None) -> None:
routing_key = Routing_Context.SESSION_KEY_PATH
if st.session_state.get(routing_key) is None and default is not None:
st.session_state[routing_key] = default
# Routing_Context.views = Routing_Context.views or {}
def redirect(self, path: str):
st.session_state[Routing_Context.SESSION_KEY_PATH] = path
st.rerun()
def route(self, *paths: str):
assert len(paths) != 0
# print(f"route paths: {paths}", flush=True)
def decorator(func: Callable):
# print("runs 2", flush=True)
def wrapper(*args, **kwargs):
# print("the decorator did actually run", flush=True)
if st.session_state.get(Routing_Context.SESSION_KEY_PATH) in paths:
# print("an actual thing is being shown")
return func(*args, **kwargs)
return lambda: None
for path in paths:
# print("I be running", flush=True)
assert isinstance(path, str)
Routing_Context.views[path] = wrapper
return wrapper
return decorator
def render(self):
for key, view in Routing_Context.views.items():
# print(f"rendering {key}:\t{view}")
view()