File size: 4,926 Bytes
7035f72 |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import streamlit as st
import json
import base64
import urllib
PATH = []
def url_from_state_dict(state):
return {
key: [json.dumps(value)]
for key, value in sorted(state.items())
}
def state_dict_from_url(params):
state = dict()
# params = st.experimental_get_query_params()
# st.write(params)
for key, value in sorted(params.items()):
state[key] = json.loads(value[-1])
return state
st.markdown('''
<style>
[data-testid="stVerticalBlock"] [data-testid="stVerticalBlock"] {
background-color: #d4d4d4;
box-shadow: 0px 0px 0px 6px #d4d4d4, 0px 0px 0px 7px #000000e3;
border-radius: 2px;
}
</style>
''', unsafe_allow_html=True)
left, right = st.columns(2)
left.write('### Session State:')
left.write(st.session_state)
right.write('### URL State:')
right.write(state_dict_from_url(st.experimental_get_query_params()))
if not st.session_state.to_dict():# state_dict_from_url(st.experimental_get_query_params()) != st.session_state.to_dict():
st.write('### Auto Setting Session State:')
print('Yass!')
print('setting to...', state_dict_from_url(st.experimental_get_query_params()))
for key, value in state_dict_from_url(st.experimental_get_query_params()).items():
st.session_state[key] = value
st.experimental_rerun()
# pass
autoset = st.sidebar.radio('Auto Set URL', ['on', 'off'], key='radio')
class NumberInput:
def __init__(
self,
label: str,
value: float,
key: str,
min_value: float = None,
max_value: float = None,
):
PATH.append(key)
self.label = label
self.path = '.'.join(PATH)
self.default = value
self.min_value = min_value
self.max_value = max_value
self.define_state('value')
PATH.pop()
def define_state(self, key):
if st.session_state.get(self.path + key) is not None:
return
st.session_state[self.path + key] = self.default
def read_state(self, key):
return st.session_state[self.path + key]
def read_value(self):
return self.read_state('value')
@property
def value(self):
return self.read_value()
def render(self):
return st.number_input(
label=self.label,
min_value=self.min_value,
max_value=self.max_value,
# value=self.default,
key=self.path + 'value',
step=0.5,
)
def argand(a):
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
for x in range(len(a)):
ax.plot([0,a[x].real],[0,a[x].imag],'ro-',label='python')
limit=np.max(np.ceil(np.absolute(a)))
ax.axis(xmin=-limit, xmax=limit, ymin=-limit, ymax=limit)
return fig
class ComplexNumberInput:
def __init__(
self,
label: str,
value: complex,
key: str,
swapplaces: bool = False,
) -> None:
PATH.append(key)
self.label = label
self.path = '.'.join(PATH)
self.default = value
# start
self.real = NumberInput('Real', self.default.real, 'real')
self.imag = NumberInput('Imaginary', self.default.imag, 'imag')
self.swapplaces = swapplaces
# end
PATH.pop()
def read_value(self):
return complex(self.real.value, self.imag.value)
@property
def value(self):
return self.read_value()
def render(self):
with st.container():
st.header(self.label)
if self.swapplaces:
self.imag.render()
self.real.render()
else:
self.real.render()
self.imag.render()
st.pyplot(argand([self.value]))
st.markdown('''
<a target="_self" href="http://localhost:8501/?complex1.imagvalue=2.0&complex1.realvalue=3.5&complex2.imagvalue=2.5&complex2.realvalue=6.0&numbervalue=0.05&radio=%22on%22">LINK</a>
''', unsafe_allow_html=True)
n = NumberInput('Number', 123, 'number')
n.render()
st.write(n.value)
left, right = st.columns(2)
with left:
x = ComplexNumberInput('Complex', 1 + 2j, 'complex1')
x.render()
st.write('`x` = ', x.value)
with right:
y = ComplexNumberInput(st.text_input('header'), 1 + 2j, 'complex2', swapplaces=st.checkbox('swap places'))
y.render()
st.write('`y` = ', y.value)
st.write('`x` + `y` = ', x.value + y.value)
if autoset == 'on':
# j = json.dumps(
# st.session_state.to_dict(),
# sort_keys=True,
# )
# escaped = urllib.parse.quote(j)
# state_dict = {
# key: json.dumps(value)
# for key, value in st.session_state.to_dict().items()
# }
# st.experimental_set_query_params(**state_dict)
st.experimental_set_query_params(
**url_from_state_dict(st.session_state.to_dict())
)
# st.experimental_rerun()
|