将插图划线嵌入到烧瓶应用程序中 [英] Embed Plotly Dash into Flask Application

查看:17
本文介绍了将插图划线嵌入到烧瓶应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地创建了我的第一个FlASK应用程序,并将我的代码划分为一系列蓝图,因为我的代码库将随着时间的推移而大幅增长。我现在正试图在我的应用程序中嵌入一个有情节的仪表盘(或者可能只是一个有情节的可视显示)。

目前,我正在使用我从网络上拿来的一个玩具例子来有条不紊地学习。第二个代码块启动DASH,但我的目标是将第二个代码集成到我的主FaskApp中。暂时,我希望它成为主应用程序中的一条路线(稍后我会将其解析为蓝图模块),但从情节丰富的文档中找不到一个说教示例来说明如何将这些整合在一起。

我寻找一些代码支持谁可能能够展示如何第二个可以无缝集成到主应用程序作为路线。

这个链接给了我一些想法来尝试,Running a Dash app within a Flask app,但我对那里提出的一些想法并不是很成功。

from flask import Flask, render_template, request, session
from flask_session import Session

app = Flask(__name__)
app.secret_key = 'abcdefg'

### Import and Register Blueprints 
from Help.routes import help
from Datatools.routes import data_tools
from Configtools.routes import config_tools
from wordAnalyzer.routes import word_analyzer
from ScoringTools.routes import test_scoring

app.register_blueprint(help)
app.register_blueprint(data_tools)
app.register_blueprint(config_tools)
app.register_blueprint(word_analyzer)
app.register_blueprint(test_scoring)

@app.route('/')
def homepage():
    return render_template('index.html')   

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

Plotly应用

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd

df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/salesfunnel.xlsx?raw=True")
pv = pd.pivot_table(df, index=['Name'], columns=["Status"], values=['Quantity'], aggfunc=sum, fill_value=0)

trace1 = go.Bar(x=pv.index, y=pv[('Quantity', 'declined')], name='Declined')
trace2 = go.Bar(x=pv.index, y=pv[('Quantity', 'pending')], name='Pending')
trace3 = go.Bar(x=pv.index, y=pv[('Quantity', 'presented')], name='Presented')
trace4 = go.Bar(x=pv.index, y=pv[('Quantity', 'won')], name='Won')

app = dash.Dash()

app.layout = html.Div(children=[
    html.H1(children='Sales Funnel Report'),
    html.Div(children='''National Sales Funnel Report.'''),
    dcc.Graph(
        id='example-graph',
        figure={
            'data': [trace1, trace2, trace3, trace4],
            'layout':
            go.Layout(title='Order Status by Customer', barmode='stack')
        })
])

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

更新

我想知道是否可以按照下面的思路做一些事情,以便产生视觉显示并将其输出到我创建的一个html文件。我的想法是,我的应用程序现在被设置为允许自定义文件输入,然后用户可以读取数据文件,它将被传递到图形。

@server.route('/fake')
def fake():
    df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/salesfunnel.xlsx?raw=True")
    pv = pd.pivot_table(df, index=['Name'], columns=["Status"], values=['Quantity'], aggfunc=sum, fill_value=0)
    trace1 = go.Bar(x=pv.index, y=pv[('Quantity', 'declined')], name='Declined')
    trace2 = go.Bar(x=pv.index, y=pv[('Quantity', 'pending')], name='Pending')
    trace3 = go.Bar(x=pv.index, y=pv[('Quantity', 'presented')], name='Presented')
    trace4 = go.Bar(x=pv.index, y=pv[('Quantity', 'won')], name='Won')
    app = dash.Dash()
    app.layout = html.Div(children=[
        html.H1(children='Sales Funnel Report'),
        html.Div(children='''National Sales Funnel Report.'''),
        dcc.Graph(
            id='example-graph',
            figure={
                'data': [trace1, trace2, trace3, trace4],
                'layout':
                go.Layout(title='Order Status by Customer', barmode='stack')
            })
    ])
    return  render_template('fake.html', dashboard = dcc.Graph) 

推荐答案

为避免混淆FlaskDash对象,我们将Flask对象重命名为server(在您的代码中都称为app),即

...
server = Flask(__name__)
...

现在,要将Dash应用程序与FlaskServer集成,只需将server作为关键字

...
app = dash.Dash(server=server, url_base_pathname="/app_base_path/")
...

url_base_pathname参数是可选的,它只允许您控制Dash应用的注册路径。

这篇关于将插图划线嵌入到烧瓶应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆