SyntaxError for function/callback

Hi,

I’m new to Dash and would appreciate any help I could get!

I’m trying to create a line-chart where you can choose a keyword from a dropdown menu, which then would display its occurrence over a time period.

I keep getting a SyntaxError and can’t figure out why. I have been looking at different threads and trying out their suggestions, but with no luck.

Here’s my code:

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 dash.dependencies import Input, Output
import os

app = dash.Dash(__name__)

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

df = pd.read_csv('PhDCount2019.csv', names = ['Keyword', 'Count', 'Year'])
df = df.dropna()

if 'DYNO' in os.environ:
    app.title = os.environ['DASH_APP_NAME']
else:
    app.title = 'dash-lineplot'

app.layout = html.Div([html.Div([html.H1("Weather Records for Seattle")], style={'textAlign': "center"}),
                        html.Div([dcc.Dropdown(id='Dropdown', multi=True, options=[
                            {'label': i, 'value': i} for i in df.Keyword.unique()
                            ]),
                        html.Div([dcc.Graph(id="my-graph")])

@app.callback(
    Output('my-graph', 'figure'),
    [Input('Dropdown', 'value')])
def update_figure(dropdown_value):
    trace = []
    for type in dropdown_value:
        trace.append(go.Scatter(x=df["Year"], y=df[type], name=type, mode='lines',
                                marker={'size': 8, "opacity": 0.6, "line": {'width': 0.5}}, ))

    layout = go.Layout(title="Temperature Variations Over Time", colorway=['#fdae61', '#abd9e9', '#2c7bb6'],
                                yaxis={"title": "Frequency"}, xaxis={"title": "Year"})

    figure ={'data': trace, 'layout': layout}
    return figure

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

I also tried a different method as suggested in a thread:

def update_figure(x, y):
    def callback(dropdown_value):
        trace = []
        for type in dropdown_value:
            trace.append(go.Scatter(x=df[x], y=df[y], name=type, mode='lines',
                                    marker={'size': 8, "opacity": 0.6, "line": {'width': 0.5}}, ))

        layout = go.Layout(title="Temperature Variations Over Time", colorway=['#fdae61', '#abd9e9', '#2c7bb6'],
                           yaxis={"title": "Frequency"}, xaxis={"title": "Year"})

        figure = {'data': trace, 'layout': layout}
        return figure
    return callback()

@app.callback(
    Output('my-graph', 'figure'),
    [Input('Dropdown', 'value')])(update_figure(df["Year"]), df["Count"])

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

At the line

html.Div([dcc.Graph(id="my-graph")])

it says "Expected ‘,’ or ‘]’

Anyone help would be very appreciated, thank you :blush:

It really helps to use indentation when typing your layout, I think you forgot to close off your DIV elements

app.layout = html.Div([
    html.Div([
        html.H1("Weather Records for Seattle")
    ], style={'textAlign': "center"}),
    html.Div([
            dcc.Dropdown(id='Dropdown', multi=True, options=[{'label': i, 'value': i} for i in df.Keyword.unique()]),
    # You didn't close off the DIV above with ])
    html.Div([
        dcc.Graph(id="my-graph")
    ])
# You didn't close off the root DIV with ])

Thanks! Appreciate it :slight_smile: