-
-
Save planetscape/a4d587f6668117321f2c0b24264eb4f6 to your computer and use it in GitHub Desktop.
Minimal example attempting to save dataframe from Bokeh selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Minimal example attempting to save dataframe from Bokeh selection (Sep 21, 2018) | |
# https://gist.github.com/jni/73a2a8b6961df6a4b1a63b1e217e66d8 | |
# save_dataframe.py | |
import pandas as pd | |
import numpy as np | |
from bokeh.server.server import Server | |
from bokeh.application import Application | |
from bokeh.application.handlers.function import FunctionHandler | |
from bokeh.plotting import figure | |
from bokeh.layouts import widgetbox, layout | |
from bokeh.models import ColumnDataSource | |
from bokeh.models.widgets import Button | |
def scatterplot(source): | |
tools = ['pan', 'box_select', 'wheel_zoom', 'reset'] | |
scatter = figure(title='scatterplot', tools=tools, | |
active_drag='box_select') | |
scatter.circle(source=source, x='x', y='y') | |
return scatter | |
def save_selected(dataframe, selection): | |
data = dataframe.iloc[selection] | |
def save(): | |
print(f'data.shape: {data.shape}') | |
# data.to_csv(r'data.csv', index = False) | |
data.to_csv(r'data.csv', index=False, encoding='utf-8') | |
return save | |
def clear_click_callbacks(button): | |
button._callbacks['clicks'].clear() # error | |
def generate_makedoc_function(dataframe): | |
def makedoc(doc): | |
source = ColumnDataSource(dataframe) | |
scatter = scatterplot(source) | |
savebutton = Button(label='download selected points') | |
savebutton.on_click(save_selected(dataframe, selection=[])) | |
def update_selection(attr, old, new): | |
# clear_click_callbacks(savebutton) # on_click appends, want replace | |
savebutton.on_click(save_selected(dataframe, | |
selection=new)) | |
source.selected.on_change('indices', update_selection) | |
page_content = layout([[scatter], [savebutton]]) | |
doc.title = 'save selected points!' | |
doc.add_root(page_content) | |
return makedoc | |
def main(path='/', port=5006): | |
df = pd.DataFrame({'x': np.random.random(100), | |
'y': np.random.random(100), | |
'useful': np.random.randint(0, 100, size=100)}) | |
apps = {path: Application(FunctionHandler(generate_makedoc_function(df)))} | |
server = Server(apps, port=port) | |
server.run_until_shutdown() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
# Minimal example attempting to save dataframe from Bokeh selection | |
# | |
# https://gist.github.com/planetscape/a4d587f6668117321f2c0b24264eb4f6 | |
# In[ ]: | |
import pandas as pd | |
import numpy as np | |
from bokeh.server.server import Server | |
from bokeh.application import Application | |
from bokeh.application.handlers.function import FunctionHandler | |
from bokeh.plotting import figure | |
from bokeh.layouts import column, widgetbox, layout | |
from bokeh.models import ColumnDataSource, Slider | |
from bokeh.models.widgets import Button, DataTable, TableColumn | |
from bokeh.themes import Theme | |
from bokeh.io import show, output_notebook | |
output_notebook() | |
# In[ ]: | |
selected_points = [] | |
def scatterplot(source): | |
tools = ['pan', 'box_select', 'wheel_zoom', 'reset'] | |
scatter = figure(title='scatterplot', | |
tools=tools, | |
active_drag='box_select') | |
scatter.circle(source=source, x='x', y='y') | |
return scatter | |
def save_selected(dataframe, selection): | |
data = dataframe.iloc[selection] | |
def save(): | |
print(f'Saving selected points: {data.shape}') | |
data.to_csv('data.csv', index=False, encoding='utf-8') | |
return save | |
def clear_click_callbacks(button): | |
button._callbacks['clicks'].clear() | |
def generate_makedoc_function(dataframe): | |
columns = [ | |
TableColumn(field="x", title="X"), | |
TableColumn(field="y", title="Y"), | |
TableColumn(field="useful", title="Useful"), | |
] | |
all_points = dataframe.index.values.tolist() | |
def makedoc(doc): | |
source = ColumnDataSource(dataframe) | |
scatter = scatterplot(source) | |
savebutton = Button(label='Download Selected Points') | |
points_filename = ".".join(["selected_points", "csv"]) | |
savebutton.on_click(save_selected(dataframe, selection=[])) | |
table = DataTable( | |
source=source, | |
columns=columns, | |
sortable=True, | |
selectable=True, | |
width=800, | |
) | |
def update_selection(attr, old, new): | |
global selected_points | |
savebutton.on_click(save_selected(dataframe, selection=new)) | |
selected_points = new | |
update_table(attr=None, old=None, new=None) | |
def update_dataframe(dataframe, selected_points): | |
dataframe = dataframe[['x', 'y', 'useful']].loc[selected_points, :] | |
return dataframe | |
def update_table(attr, old, new): | |
global selected_points | |
if len(selected_points) == 0: | |
selected_points = all_points | |
new_df = update_dataframe(dataframe, selected_points) | |
table.source = ColumnDataSource(new_df) | |
table.columns = columns | |
source.selected.on_change('indices', update_selection) | |
page_content = column([scatter, savebutton, table]) | |
doc.add_root(page_content) | |
return makedoc | |
# In[ ]: | |
df = pd.DataFrame({'x': np.random.random(100), | |
'y': np.random.random(100), | |
'useful': np.random.randint(0, 100, size=100)}) | |
show(generate_makedoc_function(df), notebook_url="http://127.0.0.1:8888") | |
# In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment