python-highcharts icon indicating copy to clipboard operation
python-highcharts copied to clipboard

Python 3.x compatibility

Open AJamesPhillips opened this issue 8 years ago • 6 comments

Using python 3.5.1, ipython 4.1.2, pip 8.1.2

$ pip install charts
$ ipython

In [1]: import charts
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-5eb3a6e89685> in <module>()
----> 1 import charts

/Users/ajp/anaconda3/lib/python3.5/site-packages/charts/__init__.py in <module>()
      3 __version__ = '0.4.6'
      4 
----> 5 from plot import plot, plotasync, line, area, spline, pie
      6 from server import run_server
      7 

ImportError: cannot import name 'plot'

Is there something I'm doing wrong. Can I help fix something? I was hoping to use this in Jupyter. Do you think this would be possible?

AJamesPhillips avatar May 18 '16 14:05 AJamesPhillips

Reworded last bit as it sounded demanding (apologies... wasn't meant to). Is there something I'm doing wrong or can I help fix something? As a side note I was hoping to use this in a Jupyter Notebook, do you think this would be possible? Many thanks!

AJamesPhillips avatar May 22 '16 15:05 AJamesPhillips

@AJamesPhillips No problem :) I'm not sure why you get that error message. In which directory are you executing the commands? You could always try to reinstall the library.

And yes, it will work in Jupyter Notebook (it was build for that purpose!) Just add show='inline' to the chart plotting command.

arnoutaertgeerts avatar May 22 '16 18:05 arnoutaertgeerts

Only works with Python 2.x so that was were I was going wrong: https://github.com/arnoutaertgeerts/python-highcharts/blob/master/charts/core.py#L25

No worries. You interested in updating python-highcharts to work with python3.x? I don't know the code base and I've never worked on updating from python2.x to python 3.x so I don't know how big a job it would be but I'm sure I could help out a bit. No worries if not.

AJamesPhillips avatar Jun 01 '16 19:06 AJamesPhillips

I too would love py3 support instead of just py2. At Pycon a couple of years ago py2to3 (I think that's the name) was starting to work. Maybe now it can mostly do it? I saw that Ubuntu is no longer shipping with py2.

I have a conda install, and I was able to get pip to work, as below. However, then charts won't open.

$ python --version 
Python 3.5.1 :: Anaconda 4.0.0 (x86_64)
$ ipython --version 
4.1.2
$ pip --version 
pip 8.1.2 from /Users/ME/anaconda/lib/python3.5/site-packages (python 3.5)
$ pip install charts
Collecting charts
  Downloading charts-0.4.6.tar.gz (51kB)
    100% | | 61kB 311kB/s 
Building wheels for collected packages: charts
  Running setup.py bdist_wheel for charts ... done
  Stored in directory: /Users/ME/Library/Caches/pip/wheels/a5/fa/66/56a32a0ef53876bf98c618d0c026f5f436f164c71efb5da84f
Successfully built charts
Installing collected packages: charts
Successfully installed charts-0.4.6

//update; I found the library I was remembering; http://python-future.org/automatic_conversion.html The future source tree includes scripts called futurize and pasteurize to aid in making Python 2 code or Python 3 code compatible with both platforms (Py2/3) using the future module. These are based on lib2to3 and use fixers from 2to3, 3to2, and python-modernize futurize passes Python 2 code through all the appropriate fixers to turn it into valid Python 3 code, and then adds future and future package imports. For conversions from Python 3 code to Py2/3, use the pasteurize script instead. This converts Py3-only constructs (e.g. new metaclass syntax) and adds future and future imports to the top of each module. In both cases, the result should be relatively clean Py3-style code that runs mostly unchanged on both Python 2 and Python 3.

AnneTheAgile avatar Jun 16 '16 01:06 AnneTheAgile

Had same issue - default conda environment for Jupyter is Python3, so charts did not work out of the box. Works fine if I instantiate a Python2 environment and install charts in that, but I don't want to go against the grain if Jupyter is leaning towards 3.

Incidentally I see the similar python-highcharts project supports Python3, but pandas support is not implemented.

Anyway, I've now converted charts to Python 3 well enough to run the Tutorial notebook without issue, and it was relatively painless in the end (though time-consuming to develop!).

  1. First run 2to3 on all py files in charts. 2to3 is installed by default with Python3 (at least with Anaconda), so it's just a matter of the right paths. I'm stuck on Windows, so I ran the "Anaconda Prompt" app and executed these two commands:
cd c:\users\heath.raftery\appdata\local\continuum\anaconda3\lib\site-packages\charts
2to3 -w chart.py core.py data.py jsonencoder.py plot.py server.py settings.py __init__.py

On non-Windows it should be a lot less painful, and 2to3 -w *.py will probably do for the second command. Incidentally I used pip show charts to find the path.

  1. Second, perform manual edits. 2to3 managed almost everything, except some errors in server.py. I manually changed the first few lines of def do_POST to this:
    def do_POST(self):
#        content_len = int(self.headers.getheader('content-length', 0))
        content_len = int(self.headers['content-length']) # modified to suit Python3 by HR170310
        str_response = self.rfile.read(content_len).decode('utf-8') #added to suit Python3 by HR170310
        # see http://stackoverflow.com/questions/6862770/python-3-let-json-object-accept-bytes-or-let-urlopen-output-strings
        body = json.loads(str_response)#self.rfile.read(content_len))

And everything is hunky-dory in the default Python3 environment of Jupyter!

Of course, these changes can't be pushed back into the repo because they'll break Python2 support, but given the relatively simple procedure I'm going to leave it as a documented procedure for now.

hraftery avatar Mar 10 '17 01:03 hraftery

I cloned this repo, did 2to3 -w *.py in charts/charts, and made the do_POST changes @hraftery suggested. Working now on Ubuntu with python 3.6

vdwees avatar Nov 07 '17 01:11 vdwees