Привет всем,
Мой Flask скрипт работает нормально на локальном хосте, однако на сервере Heroku интерактивная сетка Bokeh не отображается, и я думаю, что у меня нет сообщений об ошибках в журналах для начала. (ответ 200 и 302, который мне кажется правильным), я застрял на этой ошибке. У кого-нибудь есть идея? Большое спасибо. Я надеюсь, что мой пост не слишком беспорядочный.
Я создаю gridplot на основе двух фигур (myplot, myhist), затем я отображаю их в шаблоне html, используя script и div.
HTML
{{script | безопасно }} {{div | safe}}
Скрипт:
from flask import Flask, render_template, request, redirect, url_for, session
import os
import sys
from bokeh.embed import components
from bokeh.layouts import gridplot
from bokeh.plotting import figure
import numpy as np
### using bokeh version 2.0.1
SECRET_KEY = xxx
app = Flask(__name__)
app.secret_key = SECRET_KEY
yf_period = ['5y']
default_udl = ["DAI.DE"] # by default value
default_period = "5y" # by default value
def create_p(x, y1, y2):
TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select"
r = figure(tools=TOOLS, title="Cumulative Return", x_axis_label = "Time", y_axis_label = "Return")
r.line(x, y1, legend_label = "Equal weigths", line_width = 2, line_color = "red")
r.line(x, y2, legend_label = "Adjusted weights", line_width = 2, line_color = "green")
return r
def create_hist(y1, y2):
hist_1, bins_1 = np.histogram(y1, bins = int(np.round(len(y1)*0.25,0)))
hist_2, bins_2 = np.histogram(y2, bins = int(np.round(len(y2)*0.25,0)))
TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select,lasso_select"
p = figure(title="Distribution Return", tools=TOOLS, background_fill_color="#fafafa")
p.quad(top=hist_1, bottom=0, left=bins_1[:-1], right=bins_1[1:],
fill_color="red", line_color="red", alpha=0.5, legend_label = "Equal weigths")
p.quad(top=hist_2, bottom=0, left=bins_2[:-1], right=bins_2[1:],
fill_color="green", line_color="green", alpha=0.5, legend_label = "Adjusted weigths")
return p
@app.route('/', methods=["GET", "POST"])
def home():
if request.method == "POST":
# DO STUFF
session['period'], session['udl_name'] = default_period, default_udl
return redirect(url_for("result"))
return render_template("pages/home.html", period_names = yf_period)
@app.route('/result')
def result():
period = session.get('period', None)
udl_name = session.get('udl_name', None)
try:
x=[1,2,3,4,5,6,7
]
y1=[0.004626884,
0.01384386,
0.028751819,
0.035215938,
0.045115703,
0.043869708,
0.029449802
]
y2=[0.004626884,
0.01384386,
0.028751819,
0.035215938,
0.045115703,
0.043869708,
0.029449802
]
myplot = create_p(x,
y1,
y2
)
myhist = create_hist(
y1,
y2
)
p = gridplot([[myplot, myhist]])
# Embed plot into HTML via Flask Render
script, div = components(p)
return render_template("pages/result.html",
script = script, div = div)
except:
pass
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port = port)
Журналы:
2020-04-20T15: 57: 30.755061 + 00: 00 heroku [web .1]: Unidling 2020-04-20T15: 57: 30.757641 + 00: 00 heroku [web.1]: состояние изменено с пониженного на стартовое 2020-04-20T15: 57: 46.914016 + 00: 00 heroku [web.1]: состояние изменилось с начала до 2020-04-20T15: 57: 46.729829 + 00: 00 app [web.1]: [2020-04-20 15:57:46 +0000] [4] [INFO] Запуск gunicorn 20.0.4 2020-04-20T15: 57: 46.730457 + 00: 00 app [web.1]: [2020-04-20 15:57:46 +0000] [4] [INFO] Прослушивание: http://0.0.0.0: 7796 (4) 2020-04-20T15: 57: 46.730570 + 00: 00 app [web.1]: [2020-04-20 15:57:46 +0000] [4] [INFO] Использование worker: syn c 2020-04-20T15: 57: 46.734721 + 00: 00 app [web.1]: [2020-04-20 15:57:46 +0000] [10] [INFO] Загрузка работника с pid: 10 2020-04-20T15: 57: 46.779520 + 00: 00 app [web.1]: [2020-04-20 15:57:46 +0000] [11] [INFO] Загрузка работника с pid: 11 2020-04-20T15: 57: 50.647705 + 00: 00 heroku [router]: at = info method = GET path = "/" host = ptf-optimizer.herokuapp.com request_id = fcab3952-b3ea-4fce-b2df-51a2d524b1d0 fwd = "78.194.177.126" dyno = web.1 connect = 1ms service = 1774ms status = 200 байтов = 2006 протокол = https 2020-04-20T15: 57: 50.647433 + 00: 00 app [web.1]: 10.47.187.89 - - [20 / Apr / 2020: 15: 57: 50 +0000] "GET / HTTP / 1.1" 200 1844 " - "" Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537,36 (K HTML, как Gecko) Chrome / 70.0.3538.102 Safari / 537.36 Edge / 18.18362 " 2020-04-20T15: 57: 50.781574 + 00: 00 app [web.1]: 10.47.187.89 - - [20 / Apr / 2020: 15: 57: 50 +0000] "GET /style/style.css HTTP / 1.1 "404 635" https://ptf-optimizer.herokuapp.com/ "" Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537,36 (K HTML, как Gecko) Chrome / 70.0.3538.102 Safari / 537,36 Edge / 18,18362 " 2020-04-20T15: 57: 50.781820 + 00: 00 heroku [router]: at = info method = GET path = "/ style / style. css" host = ptf-optimizer.herokuapp.com request_id = 3b3ffe68-a4e2 -4313-94de-35278c3069de fwd = "78.194.177.126" dyno = web.1 connect = служба 1 мс = состояние 5 мс = 404 байта = протокол 803 = https 2020-04-20T15: 58: 01.585246 + 00: 00 heroku [маршрутизатор]: at = информационный метод = путь POST = "/" host = ptf-optimizer.herokuapp.com request_id = fa983e78-f32 c -44a c -87ce-1d1c189b245 c fwd = "78.194.177.126" dyno = web.1 connect = служба 1 мс = состояние 5 мс = 302 байта = протокол 579 = https 2020-04-20T15: 58: 01.585048 + 00: 00 app [web.1]: 10.47.187.89 - - [20 / Apr / 2020: 15: 58: 01 +0000] "POST / HTTP / 1.1" 302 221 " https://ptf-optimizer.herokuapp.com/ "" Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537,36 (K HTML, как Gecko) Chrome / 70.0.3538.102 Safari / 537.36 Edge / 18.18362 " 2020-04-20T15: 58: 01.682864 + 00: 00 приложение [web.1]: загрузка BNP.PA 2020-04-20T15: 58: 01.913560 + 00: 00 приложение [web.1]: 2020-04-20T15: 58: 01.977549 + 00: 00 app [web.1]: что-то делать 2020-04-20T15: 58: 02.241402 + 00: 00 heroku [маршрутизатор]: at = информационный метод = GET path = "/ result" host = ptf-optimizer.herokuapp.com request_id = ab5a90cb-c427-40e3-801f-97fb08be4d71 fwd = "78.194.177.126" dyno = web.1 connect = 2ms service = 556ms status = 200 байтов = 30750 protocol = https 2020-04-20T15: 58: 02.235865 + 00: 00 app [web.1]: 10.47.187.89 - - [20 / Apr / 2020: 15: 58: 02 +0000] "GET / result HTTP / 1.1" 200 30573 "https://ptf-optimizer.herokuapp.com/" "Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537,36 (K HTML, как Gecko) Chrome / 70.0.3538.102 Safari / 537.36 Edge / 18.18362"