-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
111 lines (88 loc) · 3.57 KB
/
main.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
import json
import requests
import streamlit as st
import time
from pathlib import Path
from distribution_zoo import (
get_random_animal_emoji,
inject_custom_css,
get_indices_from_query_params,
TextSubstitutions,
dist_mapping,
)
zoo_animal = get_random_animal_emoji()
st.set_page_config(
page_title="Distribution Zoo",
page_icon=zoo_animal,
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'Get Help': 'https://github.com/fcooper8472/distribution-zoo-v2/issues',
'Report a bug': "https://github.com/fcooper8472/distribution-zoo-v2/issues",
'About': "Explore the Distribution Zoo"
}
)
selected_class_index, selected_dist_index = get_indices_from_query_params(dist_mapping)
inject_custom_css()
if st.sidebar.button(':house: Home'):
st.experimental_set_query_params()
time.sleep(0.05)
st.rerun()
st.sidebar.title(f'Distribution Zoo {zoo_animal}')
st.sidebar.header('Distribution class:')
selected_class = st.sidebar.selectbox(
label='Select a distribution class',
options=dist_mapping.keys(),
index=selected_class_index,
placeholder='Select a distribution class',
label_visibility='collapsed',
key='dist_class',
)
st.sidebar.header('Distribution:')
if selected_class:
available_dists = dist_mapping[selected_class]
else:
available_dists = []
selected_dist = st.sidebar.selectbox(
label='Select a distribution',
options=available_dists,
index=selected_dist_index,
placeholder='Select a distribution',
label_visibility='collapsed',
key='dist',
)
if selected_dist:
selected_dist.display()
else:
with st.container():
st.title('Explore the Distribution Zoo')
st.warning('This app is under construction. Visit the [existing zoo](https://ben18785.shinyapps.io/distribution-zoo/) instead.', icon="⚠️")
with st.container():
cols = st.columns(len(dist_mapping.keys()))
for col, key in zip(cols, dist_mapping.keys()):
col.subheader(key.display_name)
for dist in dist_mapping[key]:
if col.button(dist.display_name):
st.experimental_set_query_params(
dist_class=key.short_name,
dist_name=dist.get_class_name(),
)
time.sleep(0.05)
st.rerun()
with st.container():
st.subheader('Authors:')
st.markdown(TextSubstitutions().apply_to_file(Path('homepage_authors.md')))
response_1 = requests.get('https://fcooper8472.github.io/distribution-zoo-analytics/data_30.json')
response_2 = requests.get('https://fcooper8472.github.io/distribution-zoo-analytics/data_all_time.json')
if response_1.status_code == 200 and response_2.status_code == 200:
data_month = json.loads(response_1.text)
data_all_time = json.loads(response_2.text)
substitutions = TextSubstitutions()
substitutions.add(r'{{{month_users}}}', str(data_month['user_count']))
substitutions.add(r'{{{month_sessions}}}', str(data_month['session_count']))
substitutions.add(r'{{{month_countries}}}', str(data_month['country_count']))
substitutions.add(r'{{{all_users}}}', str(data_all_time['user_count']))
substitutions.add(r'{{{all_sessions}}}', str(data_all_time['session_count']))
substitutions.add(r'{{{all_countries}}}', str(data_all_time['country_count']))
st.subheader('Analytics:')
st.markdown(substitutions.apply_to_file(Path('homepage_analytics.md')))