Task-272 Research on the basics for using Streamlit
Browse files- Pipfile +2 -0
- Pipfile.lock +3 -1
- research/Steamlit_Research.md +63 -0
- research/basic_streamlit_concepts.py +35 -0
- research/streamlit_widgets.py +31 -0
Pipfile
CHANGED
@@ -5,6 +5,8 @@ name = "pypi"
|
|
5 |
|
6 |
[packages]
|
7 |
streamlit = "*"
|
|
|
|
|
8 |
|
9 |
[dev-packages]
|
10 |
|
|
|
5 |
|
6 |
[packages]
|
7 |
streamlit = "*"
|
8 |
+
pandas = "*"
|
9 |
+
numpy = "*"
|
10 |
|
11 |
[dev-packages]
|
12 |
|
Pipfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
{
|
2 |
"_meta": {
|
3 |
"hash": {
|
4 |
-
"sha256": "
|
5 |
},
|
6 |
"pipfile-spec": 6,
|
7 |
"requires": {
|
@@ -367,6 +367,7 @@
|
|
367 |
"sha256:f4ca91d61a4bf61b0f2228f24bbfa6a9facd5f8af03759fe2a655c50ae2c6610",
|
368 |
"sha256:f6b3dfc7661f8842babd8ea07e9897fe3d9b69a1d7e5fbb743e4160f9387833b"
|
369 |
],
|
|
|
370 |
"markers": "python_version >= '3.10'",
|
371 |
"version": "==2.2.3"
|
372 |
},
|
@@ -423,6 +424,7 @@
|
|
423 |
"sha256:f3a255b2c19987fbbe62a9dfd6cff7ff2aa9ccab3fc75218fd4b7530f01efa24",
|
424 |
"sha256:fffb8ae78d8af97f849404f21411c95062db1496aeb3e56f146f0355c9989319"
|
425 |
],
|
|
|
426 |
"markers": "python_version >= '3.9'",
|
427 |
"version": "==2.2.3"
|
428 |
},
|
|
|
1 |
{
|
2 |
"_meta": {
|
3 |
"hash": {
|
4 |
+
"sha256": "c16e5f3fc76a1450c8e733093365a95a5f7d169ef54be7e83124c7fa917855da"
|
5 |
},
|
6 |
"pipfile-spec": 6,
|
7 |
"requires": {
|
|
|
367 |
"sha256:f4ca91d61a4bf61b0f2228f24bbfa6a9facd5f8af03759fe2a655c50ae2c6610",
|
368 |
"sha256:f6b3dfc7661f8842babd8ea07e9897fe3d9b69a1d7e5fbb743e4160f9387833b"
|
369 |
],
|
370 |
+
"index": "pypi",
|
371 |
"markers": "python_version >= '3.10'",
|
372 |
"version": "==2.2.3"
|
373 |
},
|
|
|
424 |
"sha256:f3a255b2c19987fbbe62a9dfd6cff7ff2aa9ccab3fc75218fd4b7530f01efa24",
|
425 |
"sha256:fffb8ae78d8af97f849404f21411c95062db1496aeb3e56f146f0355c9989319"
|
426 |
],
|
427 |
+
"index": "pypi",
|
428 |
"markers": "python_version >= '3.9'",
|
429 |
"version": "==2.2.3"
|
430 |
},
|
research/Steamlit_Research.md
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Streamlit Research:
|
2 |
+
### Creating the application:
|
3 |
+
- Import Streamlit library
|
4 |
+
|
5 |
+
```
|
6 |
+
import streamlit as st
|
7 |
+
```
|
8 |
+
|
9 |
+
- Running the Streamlit app:
|
10 |
+
- This will open a separate browser window running the application locally
|
11 |
+
```
|
12 |
+
streamlit run <app_name>.py
|
13 |
+
```
|
14 |
+
|
15 |
+
|
16 |
+
### State driven UI:
|
17 |
+
- Elements can be loaded into variables and those variables can be used to modify the specified element:
|
18 |
+
```
|
19 |
+
text_element_state = st.text('Element State Test') ## Initializes the element with this text
|
20 |
+
text_element_state.text('New Element State Test') ## Updates the displayed text
|
21 |
+
```
|
22 |
+
|
23 |
+
- Can also using caching:
|
24 |
+
- Minimizes overhead of running functions everytime the application reloads by checking the passed values to see if they have changed and only running the method if they have
|
25 |
+
```
|
26 |
+
@st.cache_data
|
27 |
+
def load_data(args):
|
28 |
+
## Some logic
|
29 |
+
|
30 |
+
text_element_state.text('Some data: (using st.cache_data)') ## This will appear immediately upon saving
|
31 |
+
```
|
32 |
+
- There are some limitations with using the cache mechanism:
|
33 |
+
- Will not work with functions with internal numerical randomization (with regards to calculations)
|
34 |
+
- Scope of the validation is within the current working directory
|
35 |
+
- Cached values are stored by reference, so it is undesirable to mutate the values
|
36 |
+
- st.write() function:
|
37 |
+
- Can pass more complex data types that will be displayed in an interactable format
|
38 |
+
- i.e., dataframes can be passed and displayed automatically as a table
|
39 |
+
- Additional functionality that is available:
|
40 |
+
- Draw histograms by using numpy:
|
41 |
+
```
|
42 |
+
st.subheader('Number of pickups by hour')
|
43 |
+
hist_values = np.histogram(
|
44 |
+
data[DATE_COLUMN].dt.hour, bins=24, range=(0,24))[0]
|
45 |
+
st.bar_chart(hist_values)
|
46 |
+
```
|
47 |
+
- Plot data on map:
|
48 |
+
```
|
49 |
+
hour_to_filter = 17
|
50 |
+
filtered_data = data[data[DATE_COLUMN].dt.hour == hour_to_filter]
|
51 |
+
st.subheader(f'Map of all pickups at {hour_to_filter}:00')
|
52 |
+
st.map(filtered_data)
|
53 |
+
```
|
54 |
+
- Filter data using slider:
|
55 |
+
```
|
56 |
+
hour_to_filter = st.slider('hour', 0, 23, 17) # min: 0h, max: 23h, default: 17h
|
57 |
+
```
|
58 |
+
- Using buttons for toggling data:
|
59 |
+
```
|
60 |
+
if st.checkbox('Show raw data'):
|
61 |
+
st.subheader('Raw data')
|
62 |
+
st.write(data)
|
63 |
+
```
|
research/basic_streamlit_concepts.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
st.write("Basic Dataframe Display")
|
6 |
+
dataframe = pd.DataFrame({
|
7 |
+
'first column': [1, 2, 3, 4],
|
8 |
+
'second column': [10, 20, 30, 40]
|
9 |
+
})
|
10 |
+
st.write(dataframe)
|
11 |
+
|
12 |
+
st.write("Numpy (Randomized) Dataframe Display")
|
13 |
+
dataframe = np.random.randn(10, 20)
|
14 |
+
st.dataframe(dataframe)
|
15 |
+
|
16 |
+
st.write("Use of Styling to highlight specific cells")
|
17 |
+
dataframe = pd.DataFrame(
|
18 |
+
np.random.randn(10, 20),
|
19 |
+
columns=('col %d' % i for i in range(20)))
|
20 |
+
|
21 |
+
st.dataframe(dataframe.style.highlight_max(axis=0))
|
22 |
+
|
23 |
+
st.write('Displaying Charts')
|
24 |
+
chart_data = pd.DataFrame(
|
25 |
+
np.random.randn(20, 3),
|
26 |
+
columns=['a', 'b', 'c'])
|
27 |
+
|
28 |
+
st.line_chart(chart_data)
|
29 |
+
|
30 |
+
st.write('Displaying Maps')
|
31 |
+
map_data = pd.DataFrame(
|
32 |
+
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
|
33 |
+
columns=['lat', 'lon'])
|
34 |
+
|
35 |
+
st.map(map_data)
|
research/streamlit_widgets.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
st.write('Sliders:')
|
6 |
+
slider_val = st.slider('Data Slider')
|
7 |
+
st.write(slider_val, 'squared is', slider_val * slider_val)
|
8 |
+
|
9 |
+
st.write('Reference by Widget Name:')
|
10 |
+
st.text_input("Text Entry", key="text")
|
11 |
+
st.session_state.text
|
12 |
+
|
13 |
+
st.write('Checkboxes:')
|
14 |
+
if st.checkbox('Show dataframe'):
|
15 |
+
chart_data = pd.DataFrame(
|
16 |
+
np.random.randn(20, 3),
|
17 |
+
columns=['a', 'b', 'c'])
|
18 |
+
|
19 |
+
chart_data
|
20 |
+
|
21 |
+
st.write('Selectboxes:')
|
22 |
+
df = pd.DataFrame({
|
23 |
+
'first column': [1, 2, 3, 4],
|
24 |
+
'second column': [10, 20, 30, 40]
|
25 |
+
})
|
26 |
+
|
27 |
+
option = st.selectbox(
|
28 |
+
'Favorite Number?',
|
29 |
+
df['first column'])
|
30 |
+
|
31 |
+
'Selected Number: ', option
|