Posts: 4
Threads: 1
Joined: Jan 2018
Hi
im a complete beginner and would greatly appreciate if you guys could help me on the below code from a tutorial. Im using Pycharm community edition.
import pandas as pd
import plotly as py
from plotly import tools
#import plotly.offline as offline
import plotly.graph_objs as go
#from plotly import tools
#import plotly.graph_objs as go
df = pd.read_csv("EURUSDhours.csv")
df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']
df.date = pd.to_datetime(df.date, format='%d.%m.%Y %H:%M:%S.%f')
df = df.set_index(df.date)
df = df[['open', 'high', 'low', 'close', 'volume']]
df = df.drop_duplicates(keep=False)
trace = go.Ohlc(x=df.index,open=df.open,high=df.high,low=df.low,close=df.close,name='Currency Quote')
data = [trace]
py.offline.plot(data,filename='tutorial.html')
Error message : "AttributeError: module 'plotly' has no attribute 'offline'"
Posts: 1,150
Threads: 42
Joined: Sep 2016
Your line #import plotly.offline as offline is commented (line starts with # symbol means it is a comment). PyCharm also colours comments differently than rest of code, so you can spot it easily.
Next time when posting code, please put code in Python code tags. And full error traceback in error code tags.
Posts: 4
Threads: 1
Joined: Jan 2018
(Jan-19-2018, 07:18 PM)j.crater Wrote: Your line #import plotly.offline as offline is commented (line starts with # symbol means it is a comment). PyCharm also colours comments differently than rest of code, so you can spot it easily.
Next time when posting code, please put code in Python code tags. And full error traceback in error code tags.
OK noted. I'm aware that #import plotly.offline as offline is commented. I was trying out solutions posted online. But it didnt work for me.
import pandas as pd
import plotly as py
from plotly import tools
import plotly.graph_objs as go
df = pd.read_csv("EURUSDhours.csv")
df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']
df.date = pd.to_datetime(df.date, format='%d.%m.%Y %H:%M:%S.%f')
df = df.set_index(df.date)
df = df[['open', 'high', 'low', 'close', 'volume']]
df = df.drop_duplicates(keep=False)
trace = go.Ohlc(x=df.index,open=df.open,high=df.high,low=df.low,close=df.close,name='Currency Quote')
data = [trace]
py.offline.plot(data,filename='tutorial.html') C:\Users\czc19\PycharmProjects\untitled1\venv\Scripts\python.exe C:/Users/czc19/Desktop/html.py
Traceback (most recent call last):
File "C:/Users/czc19/Desktop/html.py", line 2, in <module>
import plotly as py
File "C:\Users\czc19\PycharmProjects\untitled1\venv\lib\site-packages\plotly\__init__.py", line 31, in <module>
from plotly import (plotly, dashboard_objs, graph_objs, grid_objs, tools,
File "C:\Users\czc19\PycharmProjects\untitled1\venv\lib\site-packages\plotly\plotly\__init__.py", line 10, in <module>
from . plotly import (
File "C:\Users\czc19\PycharmProjects\untitled1\venv\lib\site-packages\plotly\plotly\plotly.py", line 28, in <module>
from requests.compat import json as _json
File "C:\Users\czc19\PycharmProjects\untitled1\venv\lib\site-packages\requests\__init__.py", line 97, in <module>
from . import utils
File "C:\Users\czc19\PycharmProjects\untitled1\venv\lib\site-packages\requests\utils.py", line 11, in <module>
import cgi
File "C:\Users\czc19\AppData\Local\Programs\Python\Python36-32\Lib\cgi.py", line 42, in <module>
import html
File "C:\Users\czc19\Desktop\html.py", line 17, in <module>
py.offline.plot(data,filename='tutorial.html')
AttributeError: module 'plotly' has no attribute 'offline'
Process finished with exit code 1
Posts: 1,150
Threads: 42
Joined: Sep 2016
Hmmm I see... Well I found this website. It says Plotly version should be at least 1.9.4 to use offline plotting. So do something like this to check the version (you can use Python interpreter for this simple check):
import plotly
plotly.__version__
Posts: 4
Threads: 1
Joined: Jan 2018
(Jan-20-2018, 09:30 AM)j.crater Wrote: Hmmm I see... Well I found this website. It says Plotly version should be at least 1.9.4 to use offline plotting. So do something like this to check the version (you can use Python interpreter for this simple check):
import plotly
plotly.__version__
im using the latest version 2.2.3 :(
Is there anything wrong with my python code?
Thank you.
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-20-2018, 04:48 PM
(This post was last modified: Jan-20-2018, 04:49 PM by j.crater.)
Your Python code looks fine. Can you post the CSV file, or at least copy out first say 10 lines, that will enable me to try the code myself?
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-20-2018, 05:13 PM
(This post was last modified: Jan-20-2018, 05:13 PM by j.crater.)
Nevermind, I made up some data and code runs fine for me, makes an html file and opens plot in browser. I used the code you posted in your second post.
First thing which comes to mind is to reinstall plotly, making sure you are doing it in the right Python installation or virtual environment, if you are using one.
Posts: 4
Threads: 1
Joined: Jan 2018
(Jan-20-2018, 05:13 PM)j.crater Wrote: Nevermind, I made up some data and code runs fine for me, makes an html file and opens plot in browser. I used the code you posted in your second post.
First thing which comes to mind is to reinstall plotly, making sure you are doing it in the right Python installation or virtual environment, if you are using one.
hmmm thanks for your help. I think it has to do with naming the file as html.py
It works when i deleted that file and created a new one with the same code.
Posts: 8,151
Threads: 160
Joined: Sep 2016
yes, you are right, it was issue with the name. you were shadowing html module from the standard library which is used - see lines #15-16 in your traceback. it actually imports your own script and from there things get messed...
|