首页 > 解决方案 > 从烧瓶应用程序运行破折号请求会出现 404 错误,而其他烧瓶路由运行良好

问题描述

我正在尝试将使用 dash 构建的仪表板集成到执行其他操作的烧瓶应用程序中。我希望它在http://127.0.0.1:5000/Dashboard上显示仪表板。我翻了很多资源,还是不清楚。该 URL 不断给我一个 404 Not found 错误,但所有其他烧瓶路由的 URL 都有效。

应用程序.py

from flask import Flask, render_template, request, redirect
import flask
from dashboard import *

app = Flask(__name__)
@app.route('/Recalculate', methods = ['GET','POST'])
def calculate():
    return render_template('recalculate.html', tables=[final_merged_data.to_html(classes='data', index=False)], titles=round_data.columns.values)

@app.route('/Dashboard')
def render_dashboard():
    return redirect("/pathname/")


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

仪表板.py


    import dash
    import flask
    from flask import Flask
    import dash_table
    import numpy
    import dash_core_components as dcc
    import dash_html_components as html

    external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']


    server = flask.Flask(__name__)
    app_dash = dash.Dash(__name__,server=server, routes_pathname_prefix="/pathname/", external_stylesheets=external_stylesheets)
    
    
    colors = {
        'background': '#111111',
        'text': '#7FDBFF'

    }
    # assume you have a "long-form" data frame
    # see https://plotly.com/python/px-arguments/ for more options

   
    data = [trace1, trace2]
    layout = go.Layout(barmode = 'group',xaxis_title="Day of the month", yaxis_title="Power Generated in MW")
    fig = go.Figure(data = data, layout = layout)

    fig.update_layout(
        plot_bgcolor=colors['background'],
        paper_bgcolor=colors['background'],
        font_color=colors['text']
    )
    
    app_dash.layout = html.Div(style={'backgroundColor': colors['background']}, children=[
        html.H1(
    

children='Hello George',
    style={
        'textAlign': 'center',
        'color': colors['text']
    }
),

html.Div(children='Predicted output from your wind and solar plants', style={
    'textAlign': 'center',
    'color': colors['text']
})

    if __name__ == '__main__':
        app_dash.run_server(debug=True)

标签: pythonflaskplotly-dash

解决方案


我不是专家,但我知道为此,您必须在代码中将破折号应用程序声明为函数,假设:

def init_dash(server):
    dash_app = dash.Dash(
        server=server, ...

您可以查看此网站了解更多专业细节。

干杯


推荐阅读