如何在没有valueerror的情况下将情节嵌入Bokeh Flask?

问题描述 投票:0回答:1

我是Bokeh和HTML的新手,正在努力建立一个图表,当两个过滤器发生变化时,该图表将发生变化。但是,当我通过FLASK运行代码时,遇到了valueerror,不确定我做错了什么。值错误显示为

"ValueError: Input must be a Model, a Document, a Sequence of Models and Document, or a dictionary from string to Model and Document

下面的追溯屏幕截图。如果有人能够指导我,我将不胜感激。谢谢enter image description here

我的app.py代码

import numpy as np
from bokeh.plotting import figure
import pandas as pd
from bokeh.models.tools import HoverTool
from flask import Flask, render_template, request
from bokeh.embed import components
from bokeh.io import show


x=np.random.random(100)
y=np.random.random(100)

f_12 = pd.Series(['f1', 'f2'])
f_12 = f_12.repeat(50)
f_34 = pd.Series(['f3', 'f4'])
f_34 = f_34.repeat(50)

sample_df = pd.DataFrame(data={'x': x
                          ,'y': y
                          ,'filter_12': f_12
                          ,'filter_34': f_34})

f12_list = sample_df['filter_12'].unique().tolist()
f34_list = sample_df['filter_34'].unique().tolist()


def grph(f_12, f_34):
    small_df = sample_df.loc[(sample_df['filter_12'] == f_12) & (sample_df['filter_34'] == f_34)]   
    x = list(small_df['x'])
    y = list(small_df['y'])
    fig=figure(x_axis_label="x",y_axis_label="y")
    hover=HoverTool()
    hover.tooltips = [
            ('X ','@x'),
            ('Y', '@y')]
    hover.mode = 'mouse'
    fig.add_tools(hover)
    par = np.polyfit(x, y, 1, full=True)
    slope=par[0][0]
    intercept=par[0][1]
    y_cal = [slope*i + intercept  for i in x]

    fig.line(x, y_cal, color='red', legend="Line_fit")
    fig.circle(x, y, color='green', legend='A')

    return show(fig)

app = Flask(__name__, template_folder='templates')
# Index page
@app.route('/viz2')
def index():
    f_12 = request.args.get("f_12")
    if f_12 == None:
        f_12 = "f1"
    f_34 = request.args.get("f_34")
    if f_34 == None:
        f_34 = "f3"
    # Create the plot
    plot = grph(f_12, f_34)
            # Embed plot into HTML via Flask Render
    script, div = components(plot)
    return render_template("view1.html", script=script, div=div, f12_list=f12_list, f34_list=f34_list, f_12=f_12, f_34=f_34)

if __name__ == '__main__':
    app.run(debug=True)

我的view1.html文件

<html>
<head>
<link
    href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css"
    rel="stylesheet" type="text/css">
<link
    href="http://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css"
    rel="stylesheet" type="text/css">

<script src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.js"></script>
<script src="http://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.js"></script>

</head>
<body>
<H1>Chart</H1>

<form action="/">
<select name="f_12">
    {% for f12 in f12_list %}
        {% if f12 == f_12 %}
            <option selected value="{{ f12 }}">{{ f12 }}</option> 
        {% else %} 
            <option value="{{ f12 }}">{{ f12 }}</option> 
        {% endif %}
    {% endfor %}
</select>
<select name="f_34">
    {% for f34 in f34_list %}
        {% if f34 == f_34 %}
            <option selected value="{{ f34 }}">{{ f34 }}</option> 
        {% else %} 
            <option value="{{ f34 }}">{{ f34 }}</option> 
        {% endif %}
    {% endfor %}
</select>
<input type="submit">
</form>

{{ script|safe }}
{{ div|safe }}

</body>
</html>

python flask bokeh
1个回答
0
投票

您正在这样做:

return show(fig)

但需要这样做:

return fig
© www.soinside.com 2019 - 2024. All rights reserved.