Skip to content

Instantly share code, notes, and snippets.

@quasiben
Created May 8, 2014 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quasiben/b71954485dceaf0f0ce2 to your computer and use it in GitHub Desktop.
Save quasiben/b71954485dceaf0f0ce2 to your computer and use it in GitHub Desktop.
import numpy as np
from bokeh.plotting import *
output_notebook()
def dscatter(color="black"):
def decorator(fn):
x,y = fn()
scatter(x,y, color=color, tools="pan,wheel_zoom,box_zoom,reset,resize")
show()
return decorator
@dscatter("red")
def sin_func():
N = 100
x = np.linspace(0, 4*np.pi, N)
y = np.sin(x)
return x,y
sin_func()
@spearsem
Copy link

spearsem commented May 8, 2014

This is great. The big conceptual thing we need to think about is: how would you pass an argument to dscatter that tells it "hey, listen to some UI element that will be drawn to the page and when that element is updated, take its value and stick it in as an argument to the function call" -- thus implying that whenever the UI element is updated, it automatically induces a function evaluation with the new argument.

In my previous work, we solved this by nesting decorators:

@selector(date_arg_name=DateSelector)
@provider(SomeWrappedChartApiClassName)
def my_func(date_arg_name=datetime.date(2012,12,31)):
    return CustomDataManager.perform_my_query(as_of=date_arg_name)

Assuming someone imported "SomeWrapperChartApiClassName" from a collection of created 'providers' that each wrapped (using Mako templates) some javascript/HTML, and assuming "CustomDataManager" was some existing library like Estuarial, then when a user ran "python -m webdata" within the directory where the above code lived in some .py file, it would start a server, invoke the function with the default args, render the initial graph and populate it on the page, and draw a jQuery date selector. Any time the date selector changes, that argument is sent back to the running Python kernel, the function is re-called (possibly with some memoizing or caching to speed it up) and the new data is shipped back to update the plot.

@spearsem
Copy link

spearsem commented May 8, 2014

The other problem that this solves is providing a nice developer guide / hooks for splicing in any other visualization tools out there. If you want your maps to render with Google Charts API, you just wrap the needed pandas->javascript array conversion in a document that calls Google Charts API stuff. If you want the bleeding edge of some new library, you just wrap it.

As far as Bokeh team would be concerned, it would mean we wrap anything and everything -- and especially anything that users request. Or maybe make an engine that absorbs formatted .js code and automatically creates a 'provider' out of it.

For casual users, they will just make use of whatever is built in and shipped with Bokeh. But for other vis developers, it would represent a wole system by which you can "script" a web app directly in Python, leaving off only as much javascript as precisely what's needed for the charts at hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment