使用面向对象Bokeh的多个问题 [已过时]

31



注意:此问题涉及已被弃用并已经几年没有使用的“第一代”Bokeh服务器。这个问题或其答案与任何版本的Bokeh >= 0.11都无关。
有关使用现代支持的Bokeh Server的详细信息,请参阅用户指南中的Running a Bokeh Server章节。


我正在构建一个交互式应用程序,希望了解Bokeh。我正在查看Bokeh示例,发现大多数示例都是在全局命名空间中编写的,但在“app”子目录中编写的示例采用了很好的面向对象风格,其中主要类继承自类似于HBox的Property类。
由于我认为这种编程方式并没有得到很好的文档记录,因此这将是一些混乱的问题。我遇到的第一件事是,除非我包含extra_generated_classes,否则图形不会绘制。
  1. extra_generated_classes 是什么?

    其次,看起来事件循环的 setup_events 在启动时在 create 之前被调用,并且每次图绘制触发事件时都会被调用。

  2. 为什么 setup_events 需要在每次事件触发时注册回调函数?为什么它不等待 create 完成后再尝试首次注册它们?

    我不确定的最后一件事是如何强制重绘一个 Glyph。滑块演示对我有效,我正在尝试做基本相同的事情,只是用散点图代替了线条。

    我在我的 update_data 的最后设置了一个 pdb 跟踪,我可以保证 self.sourceself.plot.renderers[-1].data_source 匹配,并且两者从一开始就被微调过了。然而,self.plot 本身并没有改变。

  3. 面向对象方法中的 store_objects 调用更新图表的等效方法是什么?

    我对第三个问题特别困惑,因为它看起来不像滑块应用程序示例需要任何类似的东西。为了澄清,我正在尝试制作可变数量的小部件/滑块,所以我的代码看起来像这样:

class属性:

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)

初始化方法

@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

更新数据

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()

请注意,我确定事件处理程序已经设置并正确触发。我只是不知道如何使更改后的源数据在散点图中反映出来。

你找到了解决这个问题的方法吗,特别是重新渲染散点图?我正在研究Bokeh是否有类似的功能。 - Jonathan Shore
10
如果你想得到答案,尽量将问题分成更小的部分。 - sorin
我投票关闭此问题,因为它涉及Bokeh软件包中已经在多年前删除的部分,仅存在于早期的1.0版本之前,并且不再得到支持或以任何方式提供。保留此问题只会给Bokeh用户带来潜在的困惑。 - bigreddot
1个回答

3

我正在寻找相同的答案(缺乏文档使事情变得困难)。

回答问题1,"extra_generated_classes"的实用性是什么:

简而言之,extra_generated_classes定义了在生成js / html代码时使用的模块名称,类名称和父级名称,并扩展了传递给应用程序类的父类(通常在示例中为HBox或VBox)。

更详细的答案。查看bokeh / server / utils / plugins.py中的源代码,这是在使用--script命令行参数传递给bokeh-server的代码运行的代码。在plugins.py的末尾,可以看到extra_generated_classes被传递给flask方法render_template,它呈现Jinja2模板。在模板oneobj.html中查看,extra_generated_classes是一个包含三个元素的数组的数组:模块名称,类名称和父级名称,这些元素被传递到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是bokeh/server/views/plugins.py中的Python代码,仅调用模板app.js的render_template。您可以在bokeh/server/templates中找到此模板。该模板使用模块名称、类名称和父级名称,基本上创建了扩展父级名称(例如HBox或VBox)到类名称(您的应用程序)的js代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接