Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import itertools
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
st.set_page_config(page_title="Streamlit Theme for Charts", page_icon="π")
|
8 |
+
|
9 |
+
|
10 |
+
def icon(emoji: str):
|
11 |
+
"""Shows an emoji as a Notion-style page icon."""
|
12 |
+
st.write(
|
13 |
+
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
|
14 |
+
unsafe_allow_html=True,
|
15 |
+
)
|
16 |
+
|
17 |
+
HEADER_COLOR_CYCLE = itertools.cycle(
|
18 |
+
[
|
19 |
+
"#00c0f2", # light-blue-70",
|
20 |
+
"#ffbd45", # "orange-70",
|
21 |
+
"#00d4b1", # "blue-green-70",
|
22 |
+
"#1c83e1", # "blue-70",
|
23 |
+
"#803df5", # "violet-70",
|
24 |
+
"#ff4b4b", # "red-70",
|
25 |
+
"#21c354", # "green-70",
|
26 |
+
"#faca2b", # "yellow-80",
|
27 |
+
]
|
28 |
+
)
|
29 |
+
|
30 |
+
|
31 |
+
def space(num_lines=1):
|
32 |
+
"""Adds empty lines to the Streamlit app."""
|
33 |
+
for _ in range(num_lines):
|
34 |
+
st.write("")
|
35 |
+
|
36 |
+
|
37 |
+
def colored_header(label, description=None, color=None):
|
38 |
+
"""Shows a header with a colored underline and an optional description."""
|
39 |
+
space(num_lines=2)
|
40 |
+
if color is None:
|
41 |
+
color = next(HEADER_COLOR_CYCLE)
|
42 |
+
st.subheader(label)
|
43 |
+
st.write(
|
44 |
+
f'<hr style="background-color: {color}; margin-top: 0; margin-bottom: 0; height: 3px; border: none; border-radius: 3px;">',
|
45 |
+
unsafe_allow_html=True,
|
46 |
+
)
|
47 |
+
if description:
|
48 |
+
st.caption(description)
|
49 |
+
|
50 |
+
|
51 |
+
icon("π")
|
52 |
+
|
53 |
+
st.title("Tab Container Prototype")
|
54 |
+
|
55 |
+
def space(num_lines=1):
|
56 |
+
"""Adds empty lines to the Streamlit app."""
|
57 |
+
for _ in range(num_lines):
|
58 |
+
st.write("")
|
59 |
+
|
60 |
+
@st.experimental_memo
|
61 |
+
def get_data():
|
62 |
+
return pd.DataFrame(np.random.randn(20, 5), columns=["a", "b", "c", "d", "e"])
|
63 |
+
|
64 |
+
if "tabs" not in st.session_state:
|
65 |
+
st.session_state["tabs"] = ["Filter Data", "Raw Data", "π Chart"]
|
66 |
+
|
67 |
+
if "tabs_sidebar" not in st.session_state:
|
68 |
+
st.session_state["tabs_sidebar"] = False
|
69 |
+
|
70 |
+
with st.expander("Show code"):
|
71 |
+
st.code("""
|
72 |
+
import streamlit as st
|
73 |
+
import pandas as pd
|
74 |
+
import numpy as np
|
75 |
+
@st.experimental_memo
|
76 |
+
def get_data():
|
77 |
+
return pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"])
|
78 |
+
tab1, tab2, tab3 = st.tabs(["Filter Data", "Raw Data", "π Chart"])
|
79 |
+
with tab1:
|
80 |
+
col = st.selectbox("Select column", options=["a", "b", "c"])
|
81 |
+
filter = st.slider("Filter range", -2.5, 2.5, (-2.0, 2.0), step=0.01)
|
82 |
+
filter_query = f"{filter[0]} < {col} < {filter[1]}"
|
83 |
+
with tab2:
|
84 |
+
st.dataframe(get_data().query(filter_query))
|
85 |
+
with tab3:
|
86 |
+
st.line_chart(get_data().query(filter_query))
|
87 |
+
""")
|
88 |
+
|
89 |
+
tabs = st.tabs(st.session_state["tabs"])
|
90 |
+
|
91 |
+
with tabs[0]:
|
92 |
+
col = st.selectbox("Select column", options=["a", "b", "c"])
|
93 |
+
filter = st.slider("Filter range", -2.5, 2.5, (-2.0, 2.0), step=0.01)
|
94 |
+
filter_query = f"{filter[0]} < {col} < {filter[1]}"
|
95 |
+
|
96 |
+
with tabs[1]:
|
97 |
+
st.dataframe(get_data().query(filter_query), height=300)
|
98 |
+
|
99 |
+
with tabs[2]:
|
100 |
+
st.line_chart(get_data().query(filter_query))
|
101 |
+
|
102 |
+
if st.session_state["tabs_sidebar"]:
|
103 |
+
tab1, tab2 = st.sidebar.tabs(["Tab 1", "Tab 2"])
|
104 |
+
data = np.random.randn(10, 1)
|
105 |
+
|
106 |
+
tab1.write("this is tab 1")
|
107 |
+
|
108 |
+
tab2.write("this is tab 2")
|
109 |
+
|
110 |
+
space(num_lines=2)
|
111 |
+
|
112 |
+
colored_header("Tabs Configruations")
|
113 |
+
|
114 |
+
new_tab = st.text_input("Tab label", "New Tab")
|
115 |
+
if st.button("Add tab"):
|
116 |
+
st.session_state["tabs"].append(new_tab)
|
117 |
+
st.experimental_rerun()
|
118 |
+
|
119 |
+
if st.button("Add tabs container to sidebar"):
|
120 |
+
st.session_state["tabs_sidebar"] = True
|
121 |
+
st.experimental_rerun()
|