-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminidataplot-streamlit.py
55 lines (42 loc) · 2.02 KB
/
minidataplot-streamlit.py
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
import streamlit as st
import pandas as pd
import plotly.express as px
# ---- Creating Dictionary of sample fuits data to model with ----
_dic = { 'Fruit Type': ['Blueberry', 'Avacado', 'Banana'], 'Quantity': [45, 28, 30]}
# Initialize page session state
if "dataloaded" not in st.session_state:
st.session_state.dataloaded = False
# setup page and columns
st.set_page_config(page_title="Streamlit Fruit Data Plot", page_icon=":bar_chart:", layout="wide")
data_column, plot_column = st.columns((1,2))
# setup initial display text
data_column.header('Fruits List')
data_column.write('This is a test page for making simple plots.')
data_column.write('Press the button to load the data dictionary.')
# setup button
load = data_column.button('Load Data')
data_column.markdown('#')
# if load button pressed now OR was pressed before, then this runs and plot choice shown
# changing plotopt by clicking re-runs page, so need to run this again with load button
if load or st.session_state.dataloaded:
_df = pd.DataFrame(_dic)
# data_column.write(_df) -> doing this shows pandas df with Index as first col
data_column.dataframe(_df, hide_index=True, use_container_width=False)
data_column.markdown('#')
st.session_state.dataloaded = True
plot_column.markdown('#')
# ---- Plot types -------
plotopt = plot_column.radio('Plot type :', ['Bar', 'Pie'])
if plotopt == 'Bar':
fig = px.bar(_df, x= 'Fruit Type', y = 'Quantity', title ='Fruit Bar Chart')
plot_column.plotly_chart(fig)
else:
fig = px.pie(_df, names = 'Fruit Type', values = 'Quantity', title ='Fruit Pie Chart')
plot_column.plotly_chart(fig)
st.write("---") # divider
# button to Reset - have to push it twice to work!
if data_column.button(label="Reset - Press x2", use_container_width=True):
# re-run by simple reset of all session state info
# delete all the items in Session state
for key in st.session_state.keys():
del st.session_state[key]