簡體   English   中英

Python Dash(Plotly):TypeError:無法散列的類型:'Div'

[英]Python Dash (Plotly): TypeError: unhashable type: 'Div'

這是代碼:它在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括號和縮進。

謝謝。

layoutmain div中,要使用多個div ,必須將主div中的所有其他潛水設置為series這些主div。

您的代碼應完全如下:

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'
            )
   ]
)

抱歉,答案太晚了,我想您可能已經解決了問題。 但是,作為針對其他發生此錯誤的人的解決方案,我在此添加它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM