🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Dash layout error

Trying to create a stock chart with Go and Dash. This code works fine just using plotly and go.
My code:

app = dash.Dash(__name__)
trace1 = go.Ohlc(
                open=df['Open'],
                high=df['High'],
                low=df['Low'],
                close=df['Close'],
                xaxis='x',
                yaxis='y',
                name='Ohlc'
)

trace2 = go.Scatter(
                y=df['Main'],
                xaxis='x2',
                yaxis='y2',
                name='Main',
                line=dict(
                    color='black',
                    width=5
                ),

               
)

trace3 = go.Scatter(
                y=df['Turbo'],
                xaxis='x2',
                yaxis='y2',
                name='Turbo',
                line=dict(
                    color='blue',
                    width=3
                ),
               
)

trace4 = go.Bar(
                y=df['Main'],
                xaxis='x2',
                yaxis='y2',
                name='Hist',
                marker = dict(
                    color=['green'
                    if (df['Main'].values[i]) > 0 and 
                       (df['Main'].values[i-1]) > 0 and
                       (df['Main'].values[i-2]) > 0 and
                       (df['Main'].values[i-3]) > 0 and
                       (df['Main'].values[i-4]) > 0 and
                       (df['Main'].values[i-5]) > 0
                    else 'red'
                    if (df['Main'].values[i]) < 0 and 
                       (df['Main'].values[i-1]) < 0 and
                       (df['Main'].values[i-2]) < 0 and
                       (df['Main'].values[i-3]) < 0 and
                       (df['Main'].values[i-4]) < 0 and
                       (df['Main'].values[i-5]) < 0
                    else 'grey'
                    for i, value in enumerate(df['Main'].values)
                    ]

                )
               
)

trace5 = go.Bar(x=df['Time'],
                y=df['Volume'],
                xaxis='x3',
                yaxis='y3',
                name='Volume',
                marker = dict(color=[
                                'green' 
                            if (value - df['Open'].values[i]) >= 0
                            else 'red'
                            for i, value in enumerate(df['Close'].values)
                        ]
                )              
)

data = [trace1, trace2, trace3, trace4, trace5]

layout = go.Layout (
    title='ES',
    font = dict(family='Droid Sans Mono'),
    margin = dict(
        l=10,
        r=10,
        b=5,
        t=10
    ),
    xaxis = dict(
        domain=[0, 1],
        rangeslider=dict(visible=False)
    ),
    yaxis = dict(
        domain = [.55, 1]
    ),
    xaxis2 = dict(
        domain = [0, 1],
        anchor = 'y2'
    ),
    yaxis2 = dict(
        domain = [.20, .50],
        anchor = 'x2'
    ),
    xaxis3 = dict(
        domain = [0, 1],
        anchor = 'y3'
    ),
    yaxis3 = dict(
        domain = [.10, .20],
        anchor = 'x3'
    )
)
fig = go.Figure(data=data, layout=layout)

app.Layout = html.Div([
    dcc.Graph(fig)
])

if __name__ == '__main__':
    app.run_server(debug=False) # (using Jupyter Notebook)

When trying to wrap in Dash, I get this error:

 * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
[2019-02-12 10:04:47,390] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "C:\Users\jeff\Anaconda3\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\flask\app.py", line 1607, in full_dispatch_request
    self.try_trigger_before_first_request_functions()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\flask\app.py", line 1654, in try_trigger_before_first_request_functions
    func()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\dash\dash.py", line 987, in _setup_server
    self._validate_layout()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\dash\dash.py", line 964, in _validate_layout
    ''
dash.exceptions.NoLayoutException: The layout was `None` at the time that `run_server` was called. Make sure to set the `layout` attribute of your application before running the server.
127.0.0.1 - - [12/Feb/2019 10:04:47] "GET / HTTP/1.1" 500 -
[2019-02-12 10:04:47,465] ERROR in app: Exception on /favicon.ico [GET]
Traceback (most recent call last):
  File "C:\Users\jeff\Anaconda3\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\flask\app.py", line 1607, in full_dispatch_request
    self.try_trigger_before_first_request_functions()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\flask\app.py", line 1654, in try_trigger_before_first_request_functions
    func()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\dash\dash.py", line 987, in _setup_server
    self._validate_layout()
  File "C:\Users\jeff\Anaconda3\lib\site-packages\dash\dash.py", line 964, in _validate_layout
    ''
dash.exceptions.NoLayoutException: The layout was `None` at the time that `run_server` was called. Make sure to set the `layout` attribute of your application before running the server.
127.0.0.1 - - [12/Feb/2019 10:04:47] "GET /favicon.ico HTTP/1.1" 500 -
​

Should be app.layout

1 Like