Skip to content

Instantly share code, notes, and snippets.

@ash2shukla
Created October 1, 2020 21:35
Show Gist options
  • Save ash2shukla/bfccfdfea0061d63a6ba5ebf06df81e4 to your computer and use it in GitHub Desktop.
Save ash2shukla/bfccfdfea0061d63a6ba5ebf06df81e4 to your computer and use it in GitHub Desktop.
Streamlit bokeh events example
import streamlit as st
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.plotting import figure
import pandas as pd
import numpy as np
from streamlit_bokeh_events import streamlit_bokeh_events
from state import provide_state
@provide_state
def main(state):
# Keep track of indices manualy
state.selected = state.selected or []
df = pd.read_csv("data.csv")
st.write(df)
source = ColumnDataSource(df)
# set indices in datasource here
source.selected.indices = state.selected
st.subheader("Select Points From Map")
plot = figure( tools="lasso_select", width=250, height=250)
plot.circle(x="x", y="y", size="size", source=source, alpha=0.6)
source.selected.js_on_change(
"indices",
CustomJS(
args=dict(source=source),
code="""
document.dispatchEvent(
new CustomEvent("TestSelectEvent", {detail: {indices: cb_obj.indices}})
)
""",
),
)
event_result = streamlit_bokeh_events(
events="TestSelectEvent",
bokeh_plot=plot,
key="foo",
debounce_time=1000,
)
# some event was thrown
if event_result is not None:
# TestSelectEvent was thrown
if "TestSelectEvent" in event_result:
st.subheader("Selected Points' Pandas Stat summary")
indices = event_result["TestSelectEvent"].get("indices", [])
state.selected = indices
st.table(df.iloc[indices].describe())
st.subheader("Raw Event Data")
st.write(event_result)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment