Skip to content

Commit

Permalink
Merge pull request #48 from AllenNeuralDynamics/han_add_pywalker
Browse files Browse the repository at this point in the history
feat: add pywalker
  • Loading branch information
hanhou authored Apr 2, 2024
2 parents 92613a3 + 1435201 commit 4a6c69c
Show file tree
Hide file tree
Showing 4 changed files with 2,587 additions and 5 deletions.
46 changes: 41 additions & 5 deletions code/Home.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import streamlit.components.v1 as components
import streamlit_nested_layout
from streamlit_plotly_events import plotly_events
from pygwalker.api.streamlit import StreamlitRenderer, init_streamlit_comm

# To suppress the warning that I set the default value of a widget and also set it in the session state
from streamlit.elements.utils import _shown_default_value_warning
Expand Down Expand Up @@ -202,6 +203,10 @@ def show_mouse_level_img_by_key_and_prefix(key, prefix, column=None, other_patte
# 'ephys_units': fetch_ephys_units,
# }

@st.cache_resource(ttl=24*3600)
def get_pyg_renderer(df, spec="./gw_config.json", **kwargs) -> "StreamlitRenderer":
return StreamlitRenderer(df, spec=spec, debug=False, **kwargs)


def draw_session_plots(df_to_draw_session):

Expand Down Expand Up @@ -530,6 +535,9 @@ def init():

st.session_state.session_stats_names = [keys for keys in st.session_state.df['sessions_bonsai'].keys()]

# Establish communication between pygwalker and streamlit
init_streamlit_comm()


def app():

Expand Down Expand Up @@ -596,16 +604,17 @@ def app():
chosen_id = stx.tab_bar(data=[
stx.TabBarItemData(id="tab_session_x_y", title="📈 Session X-Y plot", description="Interactive session-wise scatter plot"),
stx.TabBarItemData(id="tab_session_inspector", title="👀 Session Inspector", description="Select sessions from the table and show plots"),
stx.TabBarItemData(id="tab_pygwalker", title="📊 PyGWalker (Tableau)", description="Interactive dataframe explorer"),
stx.TabBarItemData(id="tab_auto_train_history", title="🎓 Automatic Training History", description="Track progress"),
stx.TabBarItemData(id="tab_auto_train_curriculum", title="📚 Automatic Training Curriculums", description="Collection of curriculums"),
# stx.TabBarItemData(id="tab_mouse_inspector", title="🐭 Mouse Inspector", description="Mouse-level summary"),
], default=st.query_params['tab_id'] if 'tab_id' in st.query_params
else st.session_state.tab_id)

placeholder = st.container()
st.session_state.tab_id = chosen_id

if chosen_id == "tab_session_x_y":
st.session_state.tab_id = chosen_id
with placeholder:
df_selected_from_plotly, x_y_cols = plot_x_y_session()

Expand All @@ -624,9 +633,39 @@ def app():
st.session_state.df_selected_from_plotly = df_selected_from_plotly
st.session_state.df_selected_from_dataframe = df_selected_from_plotly # Sync selected on dataframe
st.experimental_rerun()

elif chosen_id == "tab_pygwalker":
with placeholder:
cols = st.columns([1, 4])
cols[0].markdown('##### Exploring data using [PyGWalker](https://docs.kanaries.net/pygwalker)')
with cols[1]:
with st.expander('Specify PyGWalker json'):
# Load json from ./gw_config.json
pyg_user_json = st.text_area("Export your plot settings to json by clicking `export_code` "
"button below and then paste your json here to reproduce your plots",
key='pyg_walker', height=100)

# If pyg_user_json is not empty, use it; otherwise, use the default gw_config.json
if pyg_user_json:
try:
pygwalker_renderer = get_pyg_renderer(
df=st.session_state.df_session_filtered,
spec=pyg_user_json,
)
except:
pygwalker_renderer = get_pyg_renderer(
df=st.session_state.df_session_filtered,
spec="./gw_config.json",
)
else:
pygwalker_renderer = get_pyg_renderer(
df=st.session_state.df_session_filtered,
spec="./gw_config.json",
)

pygwalker_renderer.render_explore(height=1010, scrolling=False)

elif chosen_id == "tab_session_inspector":
st.session_state.tab_id = chosen_id
with placeholder:
with st.columns([4, 10])[0]:
if_draw_all_sessions = session_plot_settings(need_click=False)
Expand All @@ -636,18 +675,15 @@ def app():
draw_session_plots(df_to_draw_sessions)

elif chosen_id == "tab_mouse_inspector":
st.session_state.tab_id = chosen_id
with placeholder:
selected_subject_id = st.columns([1, 3])[0].selectbox('Select a mouse', options=st.session_state.df_session_filtered['subject_id'].unique())
st.markdown(f"### [Go to WaterLog](http://eng-tools:8004/water_weight_log/?external_donor_name={selected_subject_id})")

elif chosen_id == "tab_auto_train_history": # Automatic training history
st.session_state.tab_id = chosen_id
with placeholder:
add_auto_train_manager()

elif chosen_id == "tab_auto_train_curriculum": # Automatic training curriculums
st.session_state.tab_id = chosen_id
df_curriculums = st.session_state.curriculum_manager.df_curriculums().sort_values(
by=['curriculum_schema_version', 'curriculum_name', 'curriculum_version']).reset_index().drop(columns='index')
with placeholder:
Expand Down
29 changes: 29 additions & 0 deletions code/pages/3_PyGWalker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pandas as pd
import streamlit.components.v1 as components
import streamlit as st
from pygwalker.api.streamlit import init_streamlit_comm, get_streamlit_html

st.set_page_config(
page_title="Use Pygwalker In Streamlit",
layout="wide"
)

st.title("Use Pygwalker In Streamlit(support communication)")

# Initialize pygwalker communication
init_streamlit_comm()

# When using `use_kernel_calc=True`, you should cache your pygwalker html, if you don't want your memory to explode
@st.cache_resource
def get_pyg_html(df: pd.DataFrame) -> str:
# When you need to publish your application, you need set `debug=False`,prevent other users to write your config file.
html = get_streamlit_html(df, spec="./gw0.json", use_kernel_calc=False, debug=False)
return html

@st.cache_data
def get_df() -> pd.DataFrame:
return pd.read_csv("https://kanaries-app.s3.ap-northeast-1.amazonaws.com/public-datasets/bike_sharing_dc.csv")

df = get_df()

components.html(get_pyg_html(df), width=1300, height=1000, scrolling=True)
Loading

0 comments on commit 4a6c69c

Please sign in to comment.