Skip to content

Commit 4ccc93b

Browse files
committed
Added call triggers
1 parent 0184de4 commit 4ccc93b

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed

app/streamlit_app.py

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151

5252
ABOUT = f'{GIT_REPO_URL}/blob/main/README.md'
5353

54+
APP_DESC = f"""
55+
This app leverages the *Mapper Algorithm* from Topological Data Analysis (TDA) to provide an efficient and intuitive way to gain insights from your datasets.
56+
57+
For more details:
58+
**{GIT_REPO_URL}**.
59+
"""
60+
5461
# V_* are reusable values for widgets
5562

5663
V_LENS_IDENTITY = 'Identity'
@@ -113,12 +120,11 @@
113120

114121
S_RESULTS = 'stored_results'
115122

116-
APP_DESC = f"""
117-
This app leverages the *Mapper Algorithm* from Topological Data Analysis (TDA) to provide an efficient and intuitive way to gain insights from your datasets.
123+
# T_ are for call triggers
118124

119-
For more details:
120-
**{GIT_REPO_URL}**.
121-
"""
125+
T_RENDER_MAPPER = True
126+
127+
T_DRAW_MAPPER = True
122128

123129

124130
class Results:
@@ -471,6 +477,7 @@ def render_mapper_proceed():
471477
colors=X,
472478
seed=seed)
473479
st.session_state[S_RESULTS].set_mapper_plot(mapper_plot)
480+
st.session_state[T_RENDER_MAPPER] = False
474481
draw_mapper()
475482

476483

@@ -479,10 +486,12 @@ def draw_mapper():
479486
if mapper_plot is None:
480487
return
481488
colors = get_colors_data_summary()
482-
mapper_plot.update(colors=colors)
489+
seed = st.session_state[K_SEED]
490+
mapper_plot.update(colors=colors, seed=seed)
483491
mapper_fig = mapper_plot.plot()
484492
mapper_fig.update_layout(uirevision='constant')
485493
st.session_state['mapper_fig'] = mapper_fig
494+
st.session_state[T_DRAW_MAPPER] = False
486495

487496

488497
def get_colors_data_summary():
@@ -518,6 +527,9 @@ def add_data_tools():
518527
df_summary = st.session_state[S_RESULTS].df_summary
519528
if df_summary is None:
520529
df_summary = pd.DataFrame()
530+
531+
def _trigger_draw_mapper():
532+
st.session_state[T_DRAW_MAPPER] = True
521533
st.data_editor(
522534
df_summary,
523535
height=250,
@@ -529,22 +541,25 @@ def add_data_tools():
529541
width='small'),
530542
},
531543
key=K_DATA_SUMMARY,
532-
on_change=render_mapper)
544+
on_change=_trigger_draw_mapper)
533545

534546

535547
def add_plot_setting():
536-
seed = st.number_input('Seed', value=VD_SEED, key=K_SEED)
548+
def _trigger_draw_mapper():
549+
st.session_state[T_DRAW_MAPPER] = True
550+
def _trigger_render_mapper():
551+
st.session_state[T_RENDER_MAPPER] = True
552+
seed = st.number_input(
553+
'Seed',
554+
value=VD_SEED,
555+
key=K_SEED,
556+
on_change=_trigger_draw_mapper)
537557
st.toggle(
538558
'Enable 3D',
539559
value=VD_3D,
540560
key=K_ENABLE_3D,
541-
on_change=render_mapper)
561+
on_change=_trigger_render_mapper)
542562
mapper_graph = st.session_state[S_RESULTS].mapper_graph
543-
update = st.button(
544-
'🔃 Update Plot',
545-
disabled=mapper_graph is None)
546-
if update:
547-
render_mapper()
548563

549564

550565
def add_graph_plot():
@@ -582,6 +597,10 @@ def add_rendering():
582597
add_data_tools()
583598
add_plot_setting()
584599
with pl_col_1:
600+
if st.session_state.get(T_RENDER_MAPPER, True):
601+
render_mapper()
602+
if st.session_state.get(T_DRAW_MAPPER, True):
603+
draw_mapper()
585604
add_graph_plot()
586605

587606

@@ -593,12 +612,25 @@ def main():
593612
'Report a bug': REPORT_BUG,
594613
'About': ABOUT
595614
})
615+
try:
616+
logo_hori = open('app/logo_hori.png')
617+
with st.sidebar:
618+
st.image(logo_hori, use_column_width=True)
619+
st.markdown('###')
620+
st.image(logo_hori, use_column_width=True)
621+
logo_hori.close()
622+
except Exception:
623+
title = '🔮 TDA Mapper App'
624+
with st.sidebar:
625+
st.markdown(f'# {title}')
626+
st.markdown(f'# {title}')
627+
finally:
628+
with st.sidebar:
629+
st.markdown('#')
630+
st.markdown('#')
631+
596632
with st.sidebar:
597-
st.image('app/logo_hori.png', use_column_width=True)
598-
st.markdown('###')
599633
st.markdown(APP_DESC)
600-
st.image('app/logo_hori.png', use_column_width=True)
601-
st.markdown('#')
602634
if S_RESULTS not in st.session_state:
603635
st.session_state[S_RESULTS] = Results()
604636
st.markdown('#')
@@ -609,7 +641,7 @@ def main():
609641
add_rendering()
610642
st.markdown(f'''
611643
---
612-
If you found this app useful please consider putting a :star: on {GIT_REPO_URL}
644+
If you found this app useful please consider leaving a :star: on {GIT_REPO_URL}
613645
''')
614646

615647

0 commit comments

Comments
 (0)