Skip to content

Animation.to_html5_video requires python3 base64 module #5508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tomoemon opened this issue Nov 18, 2015 · 7 comments · Fixed by #5570
Closed

Animation.to_html5_video requires python3 base64 module #5508

tomoemon opened this issue Nov 18, 2015 · 7 comments · Fixed by #5570
Milestone

Comments

@tomoemon
Copy link
Contributor

In order to realize an animation in IPython note book, I have run following code. (environment: Windows 7, Python 2.7.10, matplotlib 1.5.0, IPython 4.0.0)

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

fig = plt.figure()
x = np.arange(500)
frame_list = []

for a in range(10):
    frame = plt.plot(x, a * x ** 2, lw=2)
    frame_list.append(frame)

ani = animation.ArtistAnimation(fig, frame_list, interval = 500)
from IPython.display import HTML
HTML(ani.to_html5_video())

And this code raised an AttirubteError shown below.

AttributeError                            Traceback (most recent call last)
<ipython-input-7-ad3452685e20> in <module>()
     13 ani = animation.ArtistAnimation(fig, frame_list, interval = 500)
     14 from IPython.display import HTML
---> 15 HTML(ani.to_html5_video())

C:\Users\...\Documents\projects\science\.py27\lib\site-packages\matplotlib\animation.pyc in to_html5_video(self)
    927             # Now open and base64 encode
    928             with open(f.name, 'rb') as video:
--> 929                 vid64 = base64.encodebytes(video.read())
    930                 self._base64_video = vid64.decode('ascii')
    931                 self._video_size = 'width="{0}" height="{1}"'.format(

AttributeError: 'module' object has no attribute 'encodebytes'

Although base64.encodebytes is similar to base64.encodestring, there is no such method in python2. Matplotlib doesn't support python2 now ?

Following code is my workaround for this problem.

import base64
try:
    base64.encodebytes
except AttributeError:
    base64.encodebytes = base64.encodestring
@tacaswell tacaswell added this to the Critical bugfix release (1.5.1) milestone Nov 18, 2015
@tacaswell
Copy link
Member

can you put in a PR with those changes?

There has been talk of adding python3 only features, but this is not the right place for that.

@dopplershift
Copy link
Contributor

Interesting, the Python docs (https://docs.python.org/3.5/library/base64.html) don't list encodebytes as new in Python 3.x (whereas it does list decodebytes as new for Python 3.1)

@tomoemon
Copy link
Contributor Author

@tacaswell All right. But it may take a while, because I am new to developing matplotlib.

@tacaswell
Copy link
Member

Don't worry, we all were new to mpl once!

Please reach out if you have any questions

@tomoemon
Copy link
Contributor Author

I found another problem within animation.py while testing on master branch.

The commit 728db55 has modified Animation.save method, but _init_func is not a member of Animation. So saving the ArtistAnimation raises an exception.

/home/tomoemon/project/matplotlib/.py27/lib/python2.7/site-packages/matplotlib-1.5.0+481.g6245d4e.dirty-py2.7-linux-x86_64.egg/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
    777             for anim in all_anim:
    778                 # Clear the initial frame
--> 779                 if anim._init_func:
    780                     anim._init_draw()
    781             for data in zip(*[a.new_saved_frame_seq()

AttributeError: 'ArtistAnimation' object has no attribute '_init_func'

Anyway I try to create a PR for base64 issue. (The above problem is too confusing for me for now)

@WeatherGod
Copy link
Member

@tomoemon, try working off of the latest master branch. I am pretty sure
that we fixed this particular issue recently.

On Thu, Nov 26, 2015 at 5:25 AM, tomoemon notifications@github.com wrote:

I found another problem within animation.py while testing my code.

The commit 728db55
728db55
has modified Animation.save method, but _init_func is not a member of
Animation. So saving the ArtistAnimation occurs an exception.

/home/tomoemon/project/matplotlib/.py27/lib/python2.7/site-packages/matplotlib-1.5.0+481.g6245d4e.dirty-py2.7-linux-x86_64.egg/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
777 for anim in all_anim:
778 # Clear the initial frame
--> 779 if anim._init_func:
780 anim._init_draw()
781 for data in zip(*[a.new_saved_frame_seq()

AttributeError: 'ArtistAnimation' object has no attribute '_init_func'

Anyway I try to create a PR for base64 issue. (Problem above is too
confusing for me for now)


Reply to this email directly or view it on GitHub
#5508 (comment)
.

@jenshnielsen
Copy link
Member

@WeatherGod No this is actually a new bug introduced by my fix sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants