Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple questions with Object-Oriented Bokeh [OBSOLETE]

Tags:

python

bokeh




NOTE: This question concerns the "first generation" Bokeh server, which has been deprecated and removed for several years. Nothing in this question or its answers is relevant to any version of Bokeh >= 0.11

For detailed information about using the modern, supported Bokeh Server, see the Running a Bokeh Server chapter of the User's Guide.




I'm trying to understand Bokeh for an interactive app that I'm building. I'm looking at the Bokeh examples, and I see that most of the examples are written all in the global namespace, but the ones in the "app" subdirectory are written in a nice, object-oriented style, where the main class inhereits from a Property class like HBox.

This is going to be a mish-mash of questions because I don't think this way of programming Bokeh was very well-documented. The first thing I encountered was that the plot didn't draw unless I included extra_generated_classes.

  1. What does extra_generated_classes do?

    Secondly, it looks like the event loop setup_events is called on startup before create and subsequently every time the plot triggers an event.

  2. Why does setup_events need to register callbacks each time an event is triggered? And why doesn't it wait for create to finish before attempting to register them the first time?

    The last thing I'm unsure about is how to force a redraw of a Glyph here. The slider demo works for me, and I'm trying to do basically the same thing, except with a scatterplot instead of a line.

    I set a pdb trace at the very end of my update_data, and I can guarantee that self.source matches self.plot.renderers[-1].data_source and that both of them have been tweaked from the start. However, self.plot itself doesn't change.

  3. What is the object-oriented approach's equivalent to calling store_objects to update the plot?

    I'm especially confused by this third one, because it doesn't look like the sliders_app example needs anything like that. For clarification, I'm trying to make a variable number of widgets/sliders, so this is what my code looks like:

class attributes:

extra_generated_classes = [['ScatterBias', 'ScatterBias', 'HBox']]
maxval = 100.0

inputs = Instance(bkw.VBoxForm)
outputs = Instance(bkw.VBoxForm)
plots = Dict(String, Instance(Plot))
source = Instance(ColumnDataSource)


cols = Dict(String, String)
widgets = Dict(String, Instance(bkw.Slider))
# unmodified source
df0 = Instance(ColumnDataSource)

initialize method

@classmethod
def create(cls):
    obj = cls()

    ##############################
    ## load DataFrame
    ##############################
    df = pd.read_csv('data/crime2013_tagged_clean.csv', index_col='full_name')
    obj.cols = {'x': 'Robbery', 
            'y': 'Violent crime total',
            'pop': 'Population'
            }

    cols = obj.cols

    # only keep interested values
    df2= df.ix[:, cols.values()]

    # drop empty rows
    df2.dropna(axis=0, inplace=True)

    df0 = df2.copy()
    df0.reset_index(inplace=True)
    # keep copy of original data
    obj.source = ColumnDataSource(df2)
    obj.df0 = ColumnDataSource(df0)

    ##############################
    ## draw scatterplot
    ##############################

    obj.plots = {
            'robbery': scatter(x=cols['x'],
                y=cols['y'], 
                source=obj.source,
                x_axis_label=cols['x'],
                y_axis_label=cols['y']),
            'pop': scatter(x=cols['pop'], 
                y=cols['y'], 
                source=obj.source,
                x_axis_label=cols['pop'],
                y_axis_label=cols['y'],
                title='%s by %s, Adjusted by by %s'%(cols['y'], 
                    cols['pop'], cols['pop'])),
        }

    obj.update_data()
    ##############################
    ## draw inputs
    ##############################
    # bokeh.plotting.scatter 
    ## TODO: refactor so that any number of control variables are created
    # automatically. This involves subsuming c['pop'] into c['ctrls'], which
    # would be a dictionary mapping column names to their widget titles 
    pop_slider = obj.make_widget(bkw.Slider, dict(
            start=-obj.maxval, 
            end=obj.maxval, 
            value=0, 
            step=1, 
            title='Population'), 
        cols['pop'])

    ##############################
    ## make layout
    ##############################
    obj.inputs = bkw.VBoxForm(
            children=[pop_slider]
            )

    obj.outputs = bkw.VBoxForm(
            children=[obj.plots['robbery']]
        )

    obj.children.append(obj.inputs)
    obj.children.append(obj.outputs)

    return obj

update_data

def update_data(self):
    """Update y by the amount designated by each slider"""
    logging.debug('update_data')
    c = self.cols
    ## TODO:: make this check for bad input; especially with text boxes
    betas = { 
            varname: getattr(widget, 'value')/self.maxval 
            for varname, widget in self.widgets.iteritems()
            }

    df0 = pd.DataFrame(self.df0.data)
    adj_y = []
    for ix, row in df0.iterrows():
        ## perform calculations and generate new y's
        adj_y.append(self.debias(row))

    self.source.data[c['y']] = adj_y
    assert len(adj_y) == len(self.source.data[c['x']])
    logging.debug('self.source["y"] now contains debiased data')

    import pdb; pdb.set_trace()

Note that I am sure that the event handler gets setup and triggered correctly. I just don't know how to make the changed source data reflect in the scatterplot.

like image 479
szxk Avatar asked Oct 03 '14 08:10

szxk


1 Answers

I'm searching for the same answers (lack of documentation makes it difficult).

In answer, to question #1, what is the utility of "extra_generated_classes":

tl;dr extra_generated_classes defines a modulename, classname, and parentname used in template generating js/html code, and extends the parent class passed into the app class (usually HBox or VBox in the examples).

Longer answer. Look at the source code in bokeh/server/utils/plugins.py, this is the code that is run on code passed to bokeh-server using the --script command line argument. At the end of plugins.py, you can see that extra_generated_classes is passed to the flask method render_template, which renders a Jinja2 template. Looking inside the template, oneobj.html, extra_generated_classes is an array of arrays of three things: modulename, classname, and parentname, which are passed into bokeh.server.generatejs:

{% block extra_scripts %}
  {% for modulename, classname, parentname in extra_generated_classes %}
  <script
    src="{{ url_for('bokeh.server.generatejs', modulename=modulename, classname=classname, parentname=parentname) }}"
  ></script>
  {% endfor %}
{% endblock %}

bokeh.server.generatejs is a Python code in bokeh/server/views/plugins.py, and only calls render_template for a template app.js, which you can find in bokeh/server/templates. This template takes the modulename, classname, and parentname, and basically creates js code which extends the parentname (e.g. HBox or VBox) to the classname (your app).

like image 81
Michael Avatar answered Sep 25 '22 04:09

Michael