【问题标题】:Syntax Error: "]," invalid syntax - Dash Graph Building语法错误:“]”,无效语法 - Dash Graph Building
【发布时间】:2019-07-12 14:57:26
【问题描述】:

我是 Dash 新手,我必须构建一个 Web 应用程序,在其中打印从麦克风和加速度计读取的一些数据。

现在我正在尝试了解如何设置各种图形和信息。我按照 Dash 用户指南中给出的示例进行操作。

错误出现在第 23 行,这是我设置数据和布局的行。似乎他不喜欢 ],但老实说,我不知道如何解决它。

import dash
import dash_core_components as dcc 
import dash_html_components as html 

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



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

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'), 

    html.Div(children='''Dash: A web application framework for Python.'''), 

    dcc.Graph(id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name':
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name':
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)
  • 语法错误: 文件“app.py”,第 23 行 ], ^ SyntaxError: 无效语法

为什么会出现这种错误?

P.s.:用户指南(第一个示例)-> https://dash.plot.ly/getting-started

【问题讨论】:

  • 您的'data' 元素缺少右花括号'}' 您是否在此处错误地复制和粘贴?与链接示例不同,两行的 'name' 都没有价值
  • 你的意思是这样做:data:[ {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar' , 'name':} {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name':}], ??因为现在它不喜欢 { 开头的,是的,我正确复制了代码
  • 另外,两行中“name”的值是多少?
  • 不是dash-specific,这个问题只是一个普通的Python语法错误;只需将 {figure={ 带到结束 } 并在任何 Python repl 中对其进行评估,您就会得到相同的错误。
  • 那还是无效,你添加了一个没有值的键'name',仔细看链接中的例子

标签: python syntax-error plotly-dash


【解决方案1】:

试试这个:

import dash
import dash_core_components as dcc 
import dash_html_components as html 

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



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

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'), 

    html.Div(children='''Dash: A web application framework for Python.'''), 

    dcc.Graph(id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name':'a'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name':'b'}
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )
])

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

输出

Running on http://127.0.0.1:8050/
Debugger PIN: 201-392-957
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
Running on http://127.0.0.1:8050/
Debugger PIN: 906-971-228

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2021-06-11
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多