Python Dash(Plotly):TypeError:无法散列的类型:'Div'

时间:2018-09-28 09:40:09

标签: python plotly-dash

这是代码: 它在cmd上运行,但是我在浏览器中看到的结果是一个错误。

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

df = pd.read_excel("stats.xlsx",sheet_name=0)

app = dash.Dash()
app.config['suppress_callback_exceptions'] = True
app.css.append_css({
    'external_url': 'https://cdn.rawgit.com/plotly/dash-app-stylesheets/2d266c578d2a6e8850ebce48fdb52759b2aef506/stylesheet-oil-and-gas.css',
})  # noqa: E501

app.layout = html.Div(
    html.Div([
        html.Div([
            html.H1(children='PERFORMANCE REPORT AUGUST',
                style={
                'color':'#36A9DE'
                        }, className='nine columns'),
                html.Img(
                    src="https://media.go2speed.org/brand/files/sevengames/804/asdpree.png",
                    className='three columns',
                    style={
                        'height': '12%',
                        'width': '12%',
                        'float': 'right',
                        'position': 'relative',
                        'padding-top': 0,
                        'padding-right': 0
                        },
                        ),
                html.Div(children='''*Created using Plotly Dash Python framework''',
                        className='nine columns'
                        )
                    ], className="row"
                )
            ]),

        html.Div([
            html.Div([
                dcc.Graph(
                    id='example-graph'
                    )], className='six columns'
                    )
                ], className='row'
                )
                    )

@app.callback(
    dash.dependencies.Output('example-graph', 'figure'))

def update_example_graph():
    data=go.Scatter(
        x=pd.to_datetime(df['date'].apply(lambda x: datetime.datetime.strftime(x,'%Y-%m-%d'))),
        y=round((df['revenue']-df['cost']),2),
        mode = 'markers',
        marker={
            'size': 10,
            'color': '#36A9DE',
            'line': {'width': 0.5, 'color': '#36A9DE'}
                },
        transforms=[
            dict(
                type='aggregate',
                groups=pd.to_datetime(df['date'].apply(lambda x: datetime.datetime.strftime(x,'%Y-%m-%d'))),
                aggregations=[dict(
                target='y',func='sum',enabled=True),]
                )
                    ]
    )

    return {
    'data':[data],
    'layout':go.Layout(
        xaxis=dict(
            title='Date',
            titlefont=dict(
            family='Helvetica, monospace',
            size=15)),
        yaxis=dict(
            title='Gross Profit',
            titlefont=dict(
                family='Helvetica, monospace',
                size=15))
                        )
            }

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

这是我运行上面代码的错误:

Traceback (most recent call last):
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1808, in full_dispatch_request
    self.try_trigger_before_first_request_functions()
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1855, in try_trigger_before_first_request_functions
    func()
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\dash\dash.py", line 921, in _setup_server
    self._validate_layout()
File "C:\Users\ifrolova\AppData\Local\Programs\Python\Python37\lib\site-packages\dash\dash.py", line 908, in _validate_layout
    component_ids = {layout_id} if layout_id else set()
TypeError: unhashable type: 'Div'

能否请您帮助我了解可能出了什么问题? 我检查了所有Divs括号和缩进。

谢谢。

1 个答案:

答案 0 :(得分:0)

main的{​​{1}} div中,要使用多个layout,必须将主div中的所有其他潜水设置为div这些主要的div。

您的代码应完全符合以下条件:

series

很抱歉,答案太晚了,我想您可能已经解决了问题。但是,作为对其他发生此错误的人的一种解决方案,我在这里添加它。