julien-c HF staff commited on
Commit
fe4f35b
1 Parent(s): 04da8e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +267 -0
app.py CHANGED
@@ -0,0 +1,267 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Streamlit Cheat Sheet
3
+ App to summarise streamlit docs v0.81.0 for quick reference
4
+ There is also an accompanying png version
5
+ https://github.com/daniellewisDL/streamlit-cheat-sheet
6
+ v0.71.0 November 2020 Daniel Lewis and Austin Chen
7
+ """
8
+
9
+ import streamlit as st
10
+ from pathlib import Path
11
+ import base64
12
+
13
+ # Initial page config
14
+
15
+ st.set_page_config(
16
+ page_title='Streamlit cheat sheet',
17
+ layout="wide",
18
+ initial_sidebar_state="expanded",
19
+ )
20
+
21
+ def main():
22
+ cs_sidebar()
23
+ cs_body()
24
+
25
+ return None
26
+
27
+ # Thanks to streamlitopedia for the following code snippet
28
+
29
+ def img_to_bytes(img_path):
30
+ img_bytes = Path(img_path).read_bytes()
31
+ encoded = base64.b64encode(img_bytes).decode()
32
+ return encoded
33
+
34
+ # sidebar
35
+
36
+ def cs_sidebar():
37
+
38
+ st.sidebar.header('Streamlit cheat sheet')
39
+
40
+ st.sidebar.markdown('''
41
+ <small>Summary of the [docs](https://docs.streamlit.io/en/stable/api.html), as of [Streamlit v0.81.0](https://www.streamlit.io/).</small>
42
+ ''', unsafe_allow_html=True)
43
+
44
+ st.sidebar.markdown('__How to install and import__')
45
+
46
+ st.sidebar.code('$ pip install streamlit')
47
+
48
+ st.sidebar.markdown('Import convention')
49
+ st.sidebar.code('>>> import streamlit as st')
50
+
51
+ st.sidebar.markdown('__Add widgets to sidebar__')
52
+ st.sidebar.code('''
53
+ st.sidebar.<widget>
54
+ >>> a = st.sidebar.radio(\'R:\',[1,2])
55
+ ''')
56
+
57
+ st.sidebar.markdown('__Command line__')
58
+ st.sidebar.code('''
59
+ $ streamlit --help
60
+ $ streamlit run your_script.py
61
+ $ streamlit hello
62
+ $ streamlit config show
63
+ $ streamlit cache clear
64
+ $ streamlit docs
65
+ $ streamlit --version
66
+ ''')
67
+
68
+ st.sidebar.markdown('__Pre-release features__')
69
+ st.sidebar.markdown('[Beta and experimental features](https://docs.streamlit.io/en/0.70.0/api.html#beta-and-experimental-features)')
70
+ st.sidebar.code('''
71
+ pip uninstall streamlit
72
+ pip install streamlit-nightly --upgrade
73
+ ''')
74
+
75
+ st.sidebar.markdown('''[<img src='data:image/png;base64,{}' class='img-fluid' width=32 height=32>](https://github.com/daniellewisDL/streamlit-cheat-sheet) <small>st.cheat_sheet v0.81.0 | May 2021</small>'''.format(img_to_bytes("brain.png")), unsafe_allow_html=True)
76
+
77
+ return None
78
+
79
+ ##########################
80
+ # Main body of cheat sheet
81
+ ##########################
82
+
83
+ def cs_body():
84
+ # Magic commands
85
+
86
+ col1, col2, col3 = st.beta_columns(3)
87
+
88
+ col1.subheader('Magic commands')
89
+ col1.code('''# Magic commands implicitly `st.write()`
90
+ \'\'\' _This_ is some __Markdown__ \'\'\'
91
+ a=3
92
+ 'dataframe:', data
93
+ ''')
94
+
95
+ # Display text
96
+
97
+ col1.subheader('Display text')
98
+ col1.code('''
99
+ st.text('Fixed width text')
100
+ st.markdown('_Markdown_') # see *
101
+ st.latex(r\'\'\' e^{i\pi} + 1 = 0 \'\'\')
102
+ st.write('Most objects') # df, err, func, keras!
103
+ st.write(['st', 'is <', 3]) # see *
104
+ st.title('My title')
105
+ st.header('My header')
106
+ st.subheader('My sub')
107
+ st.code('for i in range(8): foo()')
108
+ * optional kwarg unsafe_allow_html = True
109
+ ''')
110
+
111
+ # Display data
112
+
113
+ col1.subheader('Display data')
114
+ col1.code('''
115
+ st.dataframe(my_dataframe)
116
+ st.table(data.iloc[0:10])
117
+ st.json({'foo':'bar','fu':'ba'})
118
+ ''')
119
+
120
+ # Display charts
121
+
122
+ col1.subheader('Display charts')
123
+ col1.code('''
124
+ st.line_chart(data)
125
+ st.area_chart(data)
126
+ st.bar_chart(data)
127
+ st.pyplot(fig)
128
+ st.altair_chart(data)
129
+ st.vega_lite_chart(data)
130
+ st.plotly_chart(data)
131
+ st.bokeh_chart(data)
132
+ st.pydeck_chart(data)
133
+ st.deck_gl_chart(data)
134
+ st.graphviz_chart(data)
135
+ st.map(data)
136
+ ''')
137
+
138
+ # Display media
139
+
140
+ col1.subheader('Display media')
141
+ col1.code('''
142
+ st.image('./header.png')
143
+ st.audio(data)
144
+ st.video(data)
145
+ ''')
146
+
147
+ # Display interactive widgets
148
+
149
+ col2.subheader('Display interactive widgets')
150
+ col2.code('''
151
+ st.button('Hit me')
152
+ st.checkbox('Check me out')
153
+ st.radio('Radio', [1,2,3])
154
+ st.selectbox('Select', [1,2,3])
155
+ st.multiselect('Multiselect', [1,2,3])
156
+ st.slider('Slide me', min_value=0, max_value=10)
157
+ st.select_slider('Slide to select', options=[1,'2'])
158
+ st.text_input('Enter some text')
159
+ st.number_input('Enter a number')
160
+ st.text_area('Area for textual entry')
161
+ st.date_input('Date input')
162
+ st.time_input('Time entry')
163
+ st.file_uploader('File uploader')
164
+ st.color_picker('Pick a color')
165
+ ''')
166
+ col2.write('Use widgets\' returned values in variables:')
167
+ col2.code('''
168
+ >>> for i in range(int(st.number_input('Num:'))): foo()
169
+ >>> if st.sidebar.selectbox('I:',['f']) == 'f': b()
170
+ >>> my_slider_val = st.slider('Quinn Mallory', 1, 88)
171
+ >>> st.write(slider_val)
172
+ ''')
173
+
174
+ # Control flow
175
+
176
+ col2.subheader('Control flow')
177
+ col2.code('''
178
+ st.stop()
179
+ ''')
180
+
181
+ # Lay out your app
182
+
183
+ col2.subheader('Lay out your app')
184
+ col2.code('''
185
+ st.beta_container()
186
+ st.beta_columns(spec)
187
+ >>> col1, col2 = st.beta_columns(2)
188
+ >>> col1.subheader('Columnisation')
189
+ st.beta_expander('Expander')
190
+ >>> with st.beta_expander('Expand'):
191
+ >>> st.write('Juicy deets')
192
+ ''')
193
+
194
+
195
+ # Display code
196
+
197
+ col2.subheader('Display code')
198
+ col2.code('''
199
+ st.echo()
200
+ >>> with st.echo():
201
+ >>> st.write('Code will be executed and printed')
202
+ ''')
203
+
204
+ # Display progress and status
205
+
206
+ col3.subheader('Display progress and status')
207
+ col3.code('''
208
+ st.progress(progress_variable_1_to_100)
209
+ st.spinner()
210
+ >>> with st.spinner(text='In progress'):
211
+ >>> time.sleep(5)
212
+ >>> st.success('Done')
213
+ st.balloons()
214
+ st.error('Error message')
215
+ st.warning('Warning message')
216
+ st.info('Info message')
217
+ st.success('Success message')
218
+ st.exception(e)
219
+ ''')
220
+
221
+ # Placeholders, help, and options
222
+
223
+ col3.subheader('Placeholders, help, and options')
224
+ col3.code('''
225
+ st.empty()
226
+ >>> my_placeholder = st.empty()
227
+ >>> my_placeholder.text('Replaced!')
228
+ st.help(pandas.DataFrame)
229
+ st.get_option(key)
230
+ st.set_option(key, value)
231
+ st.set_page_config(layout='wide')
232
+ ''')
233
+
234
+ # Mutate data
235
+
236
+ col3.subheader('Mutate data')
237
+ col3.code('''
238
+ DeltaGenerator.add_rows(data)
239
+ >>> my_table = st.table(df1)
240
+ >>> my_table.add_rows(df2)
241
+ >>> my_chart = st.line_chart(df1)
242
+ >>> my_chart.add_rows(df2)
243
+ ''')
244
+
245
+ # Optimize performance
246
+
247
+ col3.subheader('Optimize performance')
248
+ col3.code('''
249
+ @st.cache
250
+ >>> @st.cache
251
+ ... def foo(bar):
252
+ ... # Mutate bar
253
+ ... return data
254
+ >>> # Executes d1 as first time
255
+ >>> d1 = foo(ref1)
256
+ >>> # Does not execute d1; returns cached value, d1==d2
257
+ >>> d2 = foo(ref1)
258
+ >>> # Different arg, so function d1 executes
259
+ >>> d3 = foo(ref2)
260
+ ''')
261
+
262
+ return None
263
+
264
+ # Run main()
265
+
266
+ if __name__ == '__main__':
267
+ main()