Skip to content
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

Added progress bar to video saving #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions bar_chart_race/_bar_chart_race.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import matplotlib.pyplot as plt
from matplotlib import ticker
from ._func_animation import FuncAnimation
from ._func_animation import printProgressBar
from matplotlib.colors import Colormap

from ._common_chart import CommonChart
Expand Down Expand Up @@ -461,7 +462,7 @@ def anim_func(self, i):
for text in ax.texts[start:]:
text.remove()
self.plot_bars(ax, i)

def make_animation(self):
def init_func():
ax = self.fig.axes[0]
Expand All @@ -478,10 +479,8 @@ def frame_generator(n):
for _ in range(pause):
frames.append(None)
return frames

frames = frame_generator(len(self.df_values))
anim = FuncAnimation(self.fig, self.anim_func, frames, init_func, interval=interval)

try:
fc = self.fig.get_facecolor()
if fc == (1, 1, 1, 0):
Expand All @@ -498,8 +497,11 @@ def frame_generator(n):
fc = self.fig.get_facecolor()
if fc == (1, 1, 1, 0):
fc = 'white'
print("Rendering video to " + self.filename)
ret_val = anim.save(self.filename, fps=self.fps, writer=self.writer,
savefig_kwargs=savefig_kwargs)
savefig_kwargs=savefig_kwargs, progress_callback=printProgressBar)
printProgressBar(100, 100)
print("Rendering Complete")
except Exception as e:
message = str(e)
raise Exception(message)
Expand Down
9 changes: 9 additions & 0 deletions bar_chart_race/_func_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from matplotlib import rcParams
from matplotlib import animation

def printProgressBar(current_frame, total_frames):
percent = ("{0:.2f}").format(100 * (current_frame / float(total_frames)))
filledLength = int(100 * current_frame // total_frames)
bar = '█' * filledLength + '-' * (100 - filledLength)
print(f'\r|{bar}| {percent}%', end = "\r")
# Print New Line on Complete
if current_frame == total_frames:
print()

class FuncAnimation(animation.FuncAnimation):

def to_html5_video(self, embed_limit=None, savefig_kwargs=None):
Expand Down
5 changes: 3 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Install from pypi or conda-forge

In order to save animations as mp4/m4v/mov/etc... files, you must [install ffmpeg][0], which allows for conversion to many different formats of video and audio. For macOS users, installation may be [easier using Homebrew][2].

After installation, ensure that `ffmpeg` has been added to your path by going to your command line and entering `ffmepg -version`.
For Windows, go to *Edit the system environment variables* (search from Windows menu), then navigate to *Advanced* > *Environment Variables*. In the Environment Variables window, select Path, click new and enter the `ffmpeg\bin` location you have saved in your system.
After installation, ensure that `ffmpeg` has been added to your path by going to your command line and entering `ffmpeg -version`.

## Install ImageMagick for animated gifs

Expand All @@ -21,4 +22,4 @@ Bar Chart Race requires that you have both matplotlib and pandas installed.

[0]: https://www.ffmpeg.org/download.html
[1]: https://imagemagick.org/
[2]: https://trac.ffmpeg.org/wiki/CompilationGuide/macOS#ffmpegthroughHomebrew
[2]: https://trac.ffmpeg.org/wiki/CompilationGuide/macOS#ffmpegthroughHomebrew