Trouble viewing plot with Dash

When I use the below code

   import plotly
import plotly.plotly as py
import plotly.graph_objs as go

import dash
import dash_core_components as dcc
import dash_html_components as html

import pandas as pd
from datetime import datetime


plotly.tools.set_credentials_file(username='***', api_key='***')



trace = go.Candlestick(x=date,
                open=open_price,
                high=high_price,
                low=low_price,
                close=close_price)



data = [trace]

layout = {
    'title': 'forex pairs',
    'yaxis': {'title': 'AAPL Stock'},

}
    
layout_two = {
    'shapes': [{
        'x0': '2015-12-09', 'x1': '2016-02-09',
        'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
        'line': {'color': 'rgb(30,30,30)', 'width': 1}
    }],
}

    
    
    
fig = {'data':data, 'layout':layout}
py.plot(fig, filename='aapl-recession-candlestick')

My code works as expected, and returns this in my browser,

But when I try and use Dash with the same exact code

import plotly
import plotly.plotly as py
import plotly.graph_objs as go

import dash
import dash_core_components as dcc
import dash_html_components as html

import pandas as pd
from datetime import datetime


plotly.tools.set_credentials_file(username='hockeyblades66', api_key='Ggxhe9Y4TSxmEOVKuiaR')


trace = go.Candlestick(x=date,
            open=open_price,
            high=high_price,
            low=low_price,
            close=close_price)



data = [trace]

layout = {
'title': 'forex pairs',
'yaxis': {'title': 'AAPL Stock'},

}

layout_two = {
'shapes': [{
    'x0': '2015-12-09', 'x1': '2016-02-09',
    'y0': 0, 'y1': 1, 'xref': 'x', 'yref': 'paper',
    'line': {'color': 'rgb(30,30,30)', 'width': 1}
}],
}




fig = {'data':data, 'layout':layout}
#py.plot(fig, filename='aapl-recession-candlestick')




app = dash.Dash()

app.layout = html.Div([
dcc.Graph(id="my-graph", figure=fig, config={"editable": True})
])


#if name == "main":
app.run_server(debug=True) 

My output in my console returns,

    runfile('/Users/bennicholl/Desktop/web_apps/value_area/dashboard.py', wdir='/Users/bennicholl/Desktop/web_apps/value_area')
Running on ://127.0.0.1:8050/
Debugger PIN: 593-302-216
 * Serving Flask app "dashboard" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
Traceback (most recent call last):
  File "/Users/bennicholl/Desktop/web_apps/value_area/dashboard.py", line 29, in <module>
    trace = go.Candlestick(x=date,
NameError: name 'date' is not defined
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

/Users/bennicholl/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3299: UserWarning:

To exit: use 'exit', 'quit', or Ctrl-D.

It’s claiming ‘date’ is not defined, but it obviously is. Anyone know of the reason for this happening?

Firstly you might to remove your login credentials from this example.

Secondly the error is pretty clear “name ‘date’ is not defined”, you are using variables date, open_price, high_price, low_price, and close_price in this line:

trace = go.Candlestick(x=date,
                       open=open_price,
                       high=high_price,
                       low=low_price,
                       close=close_price)

But you’ve not defined any of these variables, the error is Python telling you this isn’t valid Python code, you must define variables before you use them.

The variables are defined though. The code works perfectly fine until I add:

app = dash.Dash()

app.layout = html.Div([
dcc.Graph(id="my-graph", figure=fig, config={"editable": True})
])
    
    

app.run_server(debug=True) 

For some reason adding the above code is causing the undefined error, but again all of these variables(which are lists of prices and dates to be exact), are most definitely defined

Not in the sample code you’ve provided

I get the same error running your code with or without Dash, I get the same error just running this code:

import plotly
import plotly.plotly as py
import plotly.graph_objs as go

import dash
import dash_core_components as dcc
import dash_html_components as html

import pandas as pd
from datetime import datetime


trace = go.Candlestick(x=date,
                       open=open_price,
                       high=high_price,
                       low=low_price,
                       close=close_price)

With this Traceback:

Traceback (most recent call last):
File “C:/Users/damian/IdeaProjects/base/Dashboard/test/test20.py”, line 13, in
trace = go.Candlestick(x=date,
NameError: name ‘date’ is not defined

As an aside, I would also suggest testing your code not in an interactive shell which can cause lots of issues like this as the variables could of been defined earlier in the shell. Try running the code directly from shell/command line.

Im importing the data from another script that calls Oanda’s API.