-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c631003
commit 652eefc
Showing
6 changed files
with
92 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,67 @@ | ||
from jinja2 import Template | ||
from IPython.core.display import HTML | ||
from IPython.core.display import display, HTML | ||
import random | ||
import string | ||
import json | ||
import pkgutil | ||
import pandas as pd | ||
|
||
|
||
def generate_template(data, labels, values, **kwargs): | ||
def generate_template(data, labels, values, plot_svg, **kwargs): | ||
template = Template(data.decode("utf-8")) | ||
id_name = ''.join(random.choice(string.ascii_lowercase) for i in range(10)) | ||
output = template.render(id_name = id_name, | ||
labels = labels.tolist(), | ||
values = values.tolist(), | ||
labels = labels, | ||
values = values, | ||
kwargs = kwargs) | ||
return HTML(output) | ||
if(plot_svg): | ||
svg_id = "svg"+id_name | ||
script = """ | ||
<style> | ||
div.output_area img, div.output_area svg{ | ||
height: 100%!important; | ||
} | ||
</style> | ||
<script> | ||
var e = document.getElementById('"""+id_name+"""'); | ||
var divCheckingInterval = setInterval(function(){ | ||
if(e.getElementsByTagName('svg').length){ | ||
clearInterval(divCheckingInterval); | ||
e.getElementsByTagName('svg')[0].setAttribute("id", '"""+svg_id+"""'); | ||
var svgElement = document.getElementById('"""+svg_id+"""'); | ||
var svgString = new XMLSerializer().serializeToString(svgElement); | ||
var decoded = unescape(encodeURIComponent(svgString)); | ||
var base64 = btoa(decoded); | ||
var imgSource = `data:image/svg+xml;base64,${base64}`; | ||
e.innerHTML = "<img id='svgplot'>"; | ||
document.getElementById('svgplot').src = imgSource; | ||
}}, 1); | ||
</script> | ||
""" | ||
display(HTML(output)) | ||
display(HTML(script)) | ||
else: | ||
display(HTML(output)) | ||
|
||
def bar(labels, values, **kwargs): | ||
def bar(labels, values, plot_svg = False, **kwargs): | ||
data = pkgutil.get_data(__package__, 'templates/bar.html') | ||
return generate_template(data, labels, values, **kwargs) | ||
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs) | ||
|
||
def barh(labels, values, **kwargs): | ||
def barh(labels, values, plot_svg = False, **kwargs): | ||
data = pkgutil.get_data(__package__, 'templates/barh.html') | ||
return generate_template(data, labels, values, **kwargs) | ||
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs) | ||
|
||
def pie(labels, values, **kwargs): | ||
def pie(labels, values, plot_svg = False, **kwargs): | ||
data = pkgutil.get_data(__package__, 'templates/pie.html') | ||
return generate_template(data, labels, values, **kwargs) | ||
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs) | ||
|
||
def donut(labels, values, **kwargs): | ||
def donut(labels, values, plot_svg = False, **kwargs): | ||
data = pkgutil.get_data(__package__, 'templates/donut.html') | ||
return generate_template(data, labels, values, **kwargs) | ||
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs) | ||
|
||
def stackedbar(labels, values, **kwargs): | ||
def stackedbar(labels, values, plot_svg = False, **kwargs): | ||
data = pkgutil.get_data(__package__, 'templates/stackedbar.html') | ||
template = Template(data.decode("utf-8")) | ||
id_name = ''.join(random.choice(string.ascii_lowercase) for i in range(10)) | ||
data = [] | ||
content = [] | ||
for index, row in pd.concat([labels,values],axis=1).iterrows(): | ||
data.append(row.to_dict()) | ||
output = template.render(id_name = id_name, | ||
labels = labels.name, | ||
values = data, | ||
kwargs = kwargs) | ||
return HTML(output) | ||
content.append(row.to_dict()) | ||
generate_template(data, labels.name, content, plot_svg, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters