Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,149 +1,11 @@
|
|
1 |
-
|
2 |
-
|
|
|
3 |
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
from
|
8 |
-
|
9 |
-
from streamlit import config
|
10 |
-
|
11 |
-
|
12 |
-
class MetricsTest(unittest.TestCase):
|
13 |
-
"""Metrics Unittest class."""
|
14 |
-
|
15 |
-
def setUp(self):
|
16 |
-
"""Make sure Client singleton is always empty before starting tests."""
|
17 |
-
streamlit.metrics.Client._singleton = None
|
18 |
-
|
19 |
-
def tearDown(self):
|
20 |
-
"""Cleanup metrics client."""
|
21 |
-
config.set_option("global.metrics", False)
|
22 |
-
streamlit.metrics.Client._singleton = None
|
23 |
-
client = streamlit.metrics.Client.get_current()
|
24 |
-
client.toggle_metrics()
|
25 |
-
|
26 |
-
def test_constructor(self):
|
27 |
-
"""Test streamlit.metrics.Client."""
|
28 |
-
client = streamlit.metrics.Client()
|
29 |
-
self.assertEqual(streamlit.metrics.Client._singleton, client)
|
30 |
-
|
31 |
-
def test_get_current(self):
|
32 |
-
"""Test streamlit.metrics.clientget_current."""
|
33 |
-
client = streamlit.metrics.Client.get_current()
|
34 |
-
self.assertEqual(streamlit.metrics.Client._singleton, client)
|
35 |
-
|
36 |
-
def test_not_singleton(self):
|
37 |
-
"""Test streamlit.metrics.Client not singleton."""
|
38 |
-
client = streamlit.metrics.Client.get_current()
|
39 |
-
|
40 |
-
with pytest.raises(RuntimeError) as e:
|
41 |
-
streamlit.metrics.Client()
|
42 |
-
msg = "Client already initialized. Use .get_current() instead"
|
43 |
-
self.assertEqual(msg, str(e.value))
|
44 |
-
|
45 |
-
def test_enabled_metrics_no_prometheus(self):
|
46 |
-
"""Test streamlit.metrics.Client.toggle_metrics no prometheus."""
|
47 |
-
config.set_option("global.metrics", True)
|
48 |
-
|
49 |
-
client = streamlit.metrics.Client.get_current()
|
50 |
-
if sys.version_info <= (3, 0):
|
51 |
-
builtin_import = "__builtin__.__import__"
|
52 |
-
else:
|
53 |
-
builtin_import = "builtins.__import__"
|
54 |
-
|
55 |
-
with pytest.raises(ImportError) as e:
|
56 |
-
with patch(builtin_import, side_effect=ImportError):
|
57 |
-
client.toggle_metrics()
|
58 |
-
msg = "prometheus-client is not installed. pip install prometheus-client"
|
59 |
-
self.assertEqual(msg, str(e.value))
|
60 |
-
|
61 |
-
def test_enabled_metrics(self):
|
62 |
-
"""Test streamlit.metrics.toggle_metrics enabled."""
|
63 |
-
config.set_option("global.metrics", True)
|
64 |
-
client = streamlit.metrics.Client.get_current()
|
65 |
-
client._metrics = {}
|
66 |
-
|
67 |
-
# yapf: disable
|
68 |
-
client._raw_metrics = [
|
69 |
-
('Counter', 'unittest_counter', 'Unittest counter', []),
|
70 |
-
('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
|
71 |
-
('Gauge', 'unittest_gauge', 'Unittest gauge', []),
|
72 |
-
]
|
73 |
-
# yapf: enable
|
74 |
-
|
75 |
-
client.toggle_metrics()
|
76 |
-
|
77 |
-
client.get("unittest_counter").inc()
|
78 |
-
client.get("unittest_counter_labels").labels("some_label")
|
79 |
-
client.get("unittest_gauge").set(42)
|
80 |
-
|
81 |
-
truth = [
|
82 |
-
"unittest_counter_total 1.0",
|
83 |
-
'unittest_counter_labels_total{label="some_label"} 0.0',
|
84 |
-
"unittest_gauge 42.0",
|
85 |
-
]
|
86 |
-
lines = client.generate_latest().splitlines()
|
87 |
-
metrics = [
|
88 |
-
x.decode("utf-8") for x in lines if x.decode("utf-8").startswith("unit")
|
89 |
-
]
|
90 |
-
metrics = [str(x) for x in metrics if "_created" not in x]
|
91 |
-
self.assertEqual(sorted(truth), sorted(metrics))
|
92 |
-
|
93 |
-
def test_disabled_metrics_check_value(self):
|
94 |
-
"""Test streamlit.metrics.Client.toggle_metrics disabled check value."""
|
95 |
-
with patch("streamlit.metrics.MockMetric", spec=True) as mock_metric:
|
96 |
-
config.set_option("global.metrics", False)
|
97 |
-
client = streamlit.metrics.Client.get_current()
|
98 |
-
client._metrics = {}
|
99 |
-
|
100 |
-
# yapf: disable
|
101 |
-
client._raw_metrics = [
|
102 |
-
('Counter', 'unittest_counter', 'Unittest counter', []),
|
103 |
-
('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
|
104 |
-
('Gauge', 'unittest_gauge', 'Unittest gauge', []),
|
105 |
-
]
|
106 |
-
# yapf: enable
|
107 |
-
|
108 |
-
client.toggle_metrics()
|
109 |
-
|
110 |
-
# Test that handler in Server.py will return nothing.
|
111 |
-
self.assertEqual(client.generate_latest(), "")
|
112 |
-
|
113 |
-
client.get("unittest_counter").inc()
|
114 |
-
client.get("unittest_counter_labels").labels("some_label")
|
115 |
-
client.get("unittest_gauge").set(42)
|
116 |
-
client.get("unittest_gauge").dec()
|
117 |
-
|
118 |
-
calls = [
|
119 |
-
call(), # Constructor
|
120 |
-
call(), # unittest_counter
|
121 |
-
call(), # unittest_counter_labels
|
122 |
-
call(), # unittest_gauge
|
123 |
-
call().inc(),
|
124 |
-
call().labels("some_label"),
|
125 |
-
call().set(42),
|
126 |
-
call().dec(),
|
127 |
-
]
|
128 |
-
self.assertEqual(calls, mock_metric.mock_calls)
|
129 |
-
|
130 |
-
def test_disabled_metrics(self):
|
131 |
-
"""Test streamlit.metrics.Client.toggle_metrics disabled."""
|
132 |
-
config.set_option("global.metrics", False)
|
133 |
-
client = streamlit.metrics.Client.get_current()
|
134 |
-
client._metrics = {}
|
135 |
-
|
136 |
-
# yapf: disable
|
137 |
-
client._raw_metrics = [
|
138 |
-
('Counter', 'unittest_counter', 'Unittest counter', []),
|
139 |
-
('Counter', 'unittest_counter_labels', 'Unittest counter labels', ['label']),
|
140 |
-
('Gauge', 'unittest_gauge', 'Unittest gauge', []),
|
141 |
-
]
|
142 |
-
# yapf: enable
|
143 |
-
|
144 |
-
client.toggle_metrics()
|
145 |
-
|
146 |
-
client.get("unittest_counter").inc()
|
147 |
-
client.get("unittest_counter_labels").labels("some_label")
|
148 |
-
client.get("unittest_gauge").set(42)
|
149 |
-
client.get("unittest_gauge").dec()
|
|
|
1 |
+
@st.experimental_memo
|
2 |
+
def foo(x):
|
3 |
+
return x**2
|
4 |
|
5 |
+
if st.button("Clear Foo"):
|
6 |
+
# Clear foo's memoized values:
|
7 |
+
foo.clear()
|
8 |
|
9 |
+
if st.button("Clear All"):
|
10 |
+
# Clear values from *all* memoized functions:
|
11 |
+
st.experimental_memo.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|