为什么bokeh人物不更新新数据?

时间:2019-03-27 14:27:48

标签: python-3.x bokeh

我正在创建一个bokeh应用程序,该应用程序从Quandl个股票价格中提取数据,并根据用户输入的股票代码更改图表。我以bokeh tuorial中的示例为模型。

一切正常,但当我输入新符号时情节不会更新。

我尝试将新数据作为字典传递(在我刚刚将DataFrame传递给ColumnDataSource()之前,但是没有运气。

import pandas as pd
import numpy as np
from bokeh.models.widgets import TextInput, Select
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.layouts import column, row
from bokeh.io import show, output_notebook
import quandl

这是获取数据的功能:

def get_data(symbol):
    dictionary = {}
    data = quandl.get('WIKI/' + symbol, collapse = 'annual', returns='numpy')
    df = pd.DataFrame(data)
    dictionary['date'] = list(df.Date.values)
    dictionary['high'] = list(df.High.values)
    return dictionary

这是绘图的函数:

def modify_doc(doc):
    symbol = 'AAWW'
    source = ColumnDataSource(data = get_data(symbol))

    p = figure(x_axis_type='datetime', title='Stock Price', plot_height=350, plot_width=800)
    p.xgrid.grid_line_color=None
    p.ygrid.grid_line_alpha=0.5
    p.xaxis.axis_label = 'year'
    p.yaxis.axis_label = 'close'

    r = p.line(source.data['date'], 
               source.data['high'], 
               line_color = 'navy')

    select = Select(title="Color", value="navy", options=COLORS)
    input = TextInput(title="Ticker Symbol", value=symbol)

    def update_symbol(attrname, old, new):
        source.data = get_data(input.value)
    input.on_change('value', update_symbol)

    layout = column(row(input, width=400), row(p))

    doc.add_root(layout)

show(modify_doc)

我认为输入新符号后情节将会更新,但它保持不变。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您的代码看起来像Bokeh服务器应用程序,但是您使用show()对我来说并不好。您还试图通过将新数据分配给源来更新地物,但没有将源传递给地物对象,因此不会产生任何影响。您能尝试一下此代码是否适合您吗? (应该适用于Bokeh v1.0.4)

import random
import pandas as pd
from tornado.ioloop import IOLoop
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models.widgets import TextInput
from bokeh.layouts import column, row

def make_document(doc):
    symbol = 'AAWW'

    def get_data(symbol):
        dictionary = {}
        data = quandl.get('WIKI/' + symbol, collapse = 'annual', returns = 'numpy')
        df = pd.DataFrame(data)
        dictionary['date'] = list(df.Date.values)
        dictionary['high'] = list(df.High.values)
        return dictionary

    source = ColumnDataSource(data = get_data(symbol))

    p = figure(x_axis_type = 'datetime', title = 'Stock Price', plot_height = 350, plot_width = 800)
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_alpha = 0.5
    p.xaxis.axis_label = 'year'
    p.yaxis.axis_label = 'close'

    r = p.line(x = 'date',
               y = 'high',
               source = source,
               line_color = 'navy')

    input = TextInput(title = "Ticker Symbol", value = symbol)

    def update_symbol(attrname, old, new):
        source.data = get_data(input.value)

    input.on_change('value', update_symbol)

    layout = column(row(input, width = 400), row(p))
    doc.add_root(layout)

io_loop = IOLoop.current()
server = Server({'/myapp': Application(FunctionHandler(make_document))}, port = 5001, io_loop = io_loop)
server.start()
server.show('/myapp')
io_loop.start()

主要的变化是在这里:

r = p.line(x = 'date',
           y = 'high',
           source = source,
           line_color = 'navy')

答案 1 :(得分:0)

根据我从Tony那里得到的答案,我只需要更改一行代码:

r = p.line(x = 'date',
           y = 'high',
           source = source,
           line_color = 'navy')