Skip to content

Instantly share code, notes, and snippets.

View mjanv's full-sized avatar
🏠
Working from home

Maxime Janvier mjanv

🏠
Working from home
View GitHub Profile
@mjanv
mjanv / mock.py
Last active May 8, 2019 21:39
Python mocks in 2 minutes !
"""
"When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."
- James Whitcomb Riley (1849–1916)
"""
from unittest.mock import Mock, patch
#################
# MOCK CREATION #
#################
@mjanv
mjanv / convert_fig_to_html.py
Last active February 14, 2021 18:10
Convert Matplotlib Figure into for HTML embedding
def convert_fig_to_html(fig):
""" Convert Matplotlib figure 'fig' into a <img> tag for HTML use using base64 encoding. """
import urllib
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import StringIO
canvas = FigureCanvas(fig)
png_output = StringIO.StringIO()
canvas.print_png(png_output)
data = png_output.getvalue().encode('base64')