多页短跑应用程序

时间:2018-07-10 00:24:13

标签: python flask plotly-dash

我正在尝试构建多页Dash应用程序。当我运行以下代码时,一切正常,除了不会路由到我的/ dash_1或/ dash_2网址。想知道是否有人可以帮助我。

我的结构如下:

dash-project /   app1 /      app.py

app2 /      app.py

server.py   run.py

我的run.py代码是

from app import server as application
from app import app

import app_1.dash_1
import app_2.dash_2

我的app.py代码是:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from werkzeug.wsgi import DispatcherMiddleware
import os
import pandas as pd
import dash
import flask

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

server=flask.Flask(name)
app = dash.Dash(name)
app.config.suppress_callback_exceptions = True

app.css.append_css({
‘external_url’: ‘https://codepen.io/chriddyp/pen/bWLwgP.css’
})

我的dash_1.py代码是:

import dash 
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output 
import pandas as pd
import plotly.graph_objs as go
from analysis import * 

from app import app, server 

app = dash.Dash(name='dash_1', sharing=True, 
url_base_pathname='/dash_1', csrf_protect=False) 

app.config['suppress_callback_exceptions']=True

df = pd.read_csv('/Users/ds/app_1/main.csv')

layout = html.Div([
dcc.Graph(
    id='Senators ',
    figure={
        'data': [
            go.Scatter(
                x=df[df['candidate'] == i] 
['contributor_individual'],
                y=df[df['candidate_name'] == i]['contributor'],
                #z=df[df['candidate_name'] == i]['contributor'],
                text=df[df['candidate_name'] == i]['contributorl'],
                mode='markers',
                opacity=0.7,
                marker={
                    'size': 15,
                    'line': {'width': 0.5, 'color': 'white'}
                },
                name=i
            ) for i in df.candidate_name.unique()
        ],
        'layout': go.Layout(
            xaxis={'title': 'Contributor '},
            yaxis={'title': 'Contributor '},
            #margin={'l': 40, 'b': 40, 't': 10, 'r': 10},
            #legend={'x': 0, 'y': 1},
            hovermode='closest'
            )
        }
    )
])

layout = html.Div(children=[
html.H1(children='Senators '),

html.Div(children='''
    God Bless.
'''),

dcc.Graph(
    id='main.csv',
    figure={
        'data': [
        {'x':df['candidate'], 'y': df['sectorl'], 'type': 'bar'},
        {'x':df['candidate'], 'y': df['industry'], 'type': 'bar'},
        {'x':df['candidate'], 'y': df['contributor'], 'type': 'bar'},
        ],
        'layout': {
            'title': 'Let Them Eat...'
            }
        }
    )
])

我的dash_2.py代码是

import dash 
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd 

from app import app, server  

app = dash.Dash(name='dash_2', sharing=True, 
url_base_pathname='/dash_2', csrf_protect=False) 

app.config['suppress_callback_exceptions']=True 

df = pd.read_csv('/Users/ds/app_2/slice.csv')



layout = html.Div(children=[
html.H1(children='Bars'),

html.Div(children='''
    One Bite....
'''),

dcc.Graph(
    id='slice.csv',
    figure={
        'data': [
        {'x':df['Slice'], 'y': df['Score'], 'type': 'bar'},
        ],
        'layout': {
            'title': 'Bars'
            }
        }
    )


])

任何帮助将不胜感激-我似乎对此不知所措-谢谢!

2 个答案:

答案 0 :(得分:2)

您需要将烧瓶应用程序传递到Dash实例。 这样。

app = dash.Dash(__name__, server=server)

因此,请尝试导入服务器,并将其作为参数包含在app1和app2中的Dash实例中。 像这样:

from app import server

app = dash.Dash(name='dash_1', sharing=True, 
url_base_pathname='/dash_1', csrf_protect=False, server=server)

app = dash.Dash(name='dash_2', sharing=True, 
url_base_pathname='/dash_2', csrf_protect=False, server=server)  

希望有帮助。

答案 1 :(得分:0)

@Franrey Saycon是完全正确的。您导入了太多东西。如果这对任何人都有用,我创建了一个tutorial that shows how to build a multipage Dash application,其中包含有关文件夹/文件结构和库导入的说明。

multipage dash application