我是Bokeh和HTML的新手,并且正在努力建立一个图表,该图表在两个过滤器发生变化时会发生变化。但是,当我通过FLASK运行代码时,在另一个窗口上出现URL找不到错误。下面是输入页面(第一个屏幕截图),如果我单击“提交”,另一个URL正在加载并说“找不到URL”(第二个屏幕截图),则不确定我在做什么错。我需要在同一页面上输入和输出,如果有人能够指导我,我将不胜感激。谢谢
第一个屏幕截图
第二张屏幕截图
我的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 fig (#edited this from show(fig) to 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-1.1.0.min.css"
rel="stylesheet" type="text/css">
<link
href="http://cdn.pydata.org/bokeh/release/bokeh-widgets-1.1.0.min.css"
rel="stylesheet" type="text/css">
<script src="http://cdn.pydata.org/bokeh/release/bokeh-1.1.0.min.js"></script>
<script src="http://cdn.pydata.org/bokeh/release/bokeh-widgets-1.1.0.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>
答案 0 :(得分:1)
您正在这样做:
return show(fig)
但是需要这样做:
return fig