-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_app.py
135 lines (95 loc) · 5.18 KB
/
core_app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import streamlit as st
import numpy as np
from graph_dict import get_graph_dicts
from data_treatment import file_to_dataframe
from data_utils import create_color_palettes, get_file_extension, filter_data, sepoptions
from components_utils import load_components, get_selectors, get_filters_out
from altair_code.time_spacial import geographic_alt
def core_app(app_version, custom):
if 'file_uploaded' not in st.session_state:
file_ext = get_file_extension(app_version)
# this is the first thing to show up when the app loads
upl1, right_side = st.columns((5,1))
with upl1:
file = upl1.file_uploader(label='upload your file', type=[file_ext], accept_multiple_files=False)
if custom:
with right_side:
separator = st.selectbox(label='csv separator', options=sepoptions, format_func=lambda x : 'tab' if (x == '\t') else x, index=2)
custom_mapping_file = st.file_uploader(label='upload your custom mapping (refresh the page for explanations)', type=['csv'], accept_multiple_files=False)
button_pressed = st.button(label='load application')
if file != None and button_pressed:
st.session_state['file'] = file
st.session_state['file_uploaded'] = 'true'
if custom:
if custom_mapping_file != None:
st.session_state['custom_mapping_file'] = custom_mapping_file
st.session_state['separator'] = separator
st.experimental_rerun()
else:
if 'data_loaded' not in st.session_state:
# this sections plays when you just uploaded your data
#loading the pandas dataframe from the excel or csv file provided
data = file_to_dataframe(st.session_state, app_version)
st.session_state['data'] = data
# streamlit has to load the custom components
family_selector, type_selector = load_components()
st.session_state['family_selector'] = family_selector
st.session_state['type_selector'] = type_selector
#color_palettes are loaded, if custom data is used, then a custom function creates the color palettes
#otherwise, pre defined color palettes are used
colors = create_color_palettes(data, app_version)
st.session_state['families'], st.session_state['orders'] = colors
#components specific to the app version are initialised and stored in a list variable
st.session_state['selectors_components'] = get_selectors(data, app_version, colors)
#getting years for the slider
years = np.unique(st.session_state['data']['year_collected'].to_numpy())
years = np.array(years, dtype='int')
years = years[(years <= 2800) & (years > 1700)]
st.session_state['min_year'] = int(min(years))
st.session_state['max_year'] = int(max(years))
#the graphs available for this app_version are loaded
st.session_state["graphs_time"], st.session_state["graphs_space"] = get_graph_dicts(app_version)
# the data is now loaded and the app is ready to work
st.session_state['data_loaded'] = "true"
# this plays whenever the app is refreshed and the data has already been loaded,
# I put a lot of variables in session_state so that it does not have to compute them again on each rerun
min_year = st.session_state['min_year']
max_year = st.session_state['max_year']
data = st.session_state['data']
graphs_time = st.session_state["graphs_time"]
graphs_space = st.session_state["graphs_space"]
family_selector = st.session_state['family_selector']
type_selector = st.session_state['type_selector']
families = st.session_state['families']
orders = st.session_state['orders']
selectors_components = st.session_state['selectors_components']
# variable declarations
default_min_year = 1930
# placing selectors in the sidebar
selectors = st.sidebar.container()
with selectors:
st.title('family and type filters')
list_filter_out = get_filters_out(selectors_components)
# main layout definition
title_col, _ = st.columns((4,1))
space_col1, space_col2 = st.columns((1,1))
select1, select2, select3 = st.columns((4,2,2))
time_col1, = st.columns(1)
# main selectors : a slider and two graph selectors
with select1:
time1, time2 = st.slider(label='time selector', min_value= min_year, max_value= max_year, value=(default_min_year, max_year))
with select2:
create_chart_time = graphs_time[st.selectbox(label='choose a time graph', options=list(graphs_time.keys()))]
with select3:
create_chart_space = graphs_space[st.selectbox(label='choose a spatial graph', options=list(graphs_space.keys()))]
# filtering data according to all selectors
filtered_data = filter_data(data, list_filter_out, time1, time2)
# creating the charts using the filtered data
chart_time = create_chart_time(filtered_data, app_version, (families, orders))
chart_space1 = geographic_alt(filtered_data, app_version, (families, orders))
chart_space2 = create_chart_space(filtered_data, app_version, (families, orders))
# drawing the graphs using the streamlit dedicated altair API
time_col1.altair_chart(chart_time, True)
space_col1.altair_chart(chart_space1, True)
space_col2.altair_chart(chart_space2, True)
# all done