Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def render(self, **kwargs):
"""Renders the HTML representation of the element."""
figure = self.get_root()
assert isinstance(figure, Figure), ("You cannot render this Element "
"if it's not in a Figure.")
header = self._template.module.__dict__.get('header', None)
if header is not None:
figure.header.add_children(Element(header(self, kwargs)),
name=self.get_name())
html = self._template.module.__dict__.get('html', None)
if html is not None:
figure.html.add_children(Element(html(self, kwargs)),
name=self.get_name())
script = self._template.module.__dict__.get('script', None)
if script is not None:
figure.script.add_children(Element(script(self, kwargs)),
name=self.get_name())
for name, element in self._children.items():
element.render(**kwargs)
""")
# Create the meta tag.
self.header.add_children(Element(
''), # noqa
name='meta_http')
# Import Javascripts
for name, url in _default_js:
self.header.add_children(JavascriptLink(url), name=name)
# Import Css
for name, url in _default_css:
self.header.add_children(CssLink(url), name=name)
self.header.add_children(Element("""
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#map {
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
}</style>
def __init__(self, html=None, width="100%", height=None, ratio="60%",
figsize=None):
super(IFrame, self).__init__()
self._name = 'IFrame'
self.width = width
self.height = height
self.ratio = ratio
if figsize is not None:
self.width = str(60*figsize[0])+'px'
self.height = str(60*figsize[1])+'px'
if isinstance(html, text_type) or isinstance(html, binary_type):
self.add_children(Element(html))
elif html is not None:
self.add_children(html)
def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
super(Figure, self).__init__()
self._name = 'Figure'
self.header = Element()
self.html = Element()
self.script = Element()
self.header._parent = self
self.html._parent = self
self.script._parent = self
self.width = width
self.height = height
self.ratio = ratio
if figsize is not None:
self.width = str(60*figsize[0])+'px'
self.height = str(60*figsize[1])+'px'
self._template = Template(u"""
("bootstrap_theme_css",
"https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"), # noqa
("awesome_markers_font_css",
"https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"), # noqa
("awesome_markers_css",
"https://rawgit.com/lvoogdt/Leaflet.awesome-markers/2.0/develop/dist/leaflet.awesome-markers.css"), # noqa
("marker_cluster_default_css",
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css"), # noqa
("marker_cluster_css",
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css"), # noqa
("awesome_rotate_css",
"https://raw.githubusercontent.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css"), # noqa
]
class Figure(Element):
"""Create a Figure object, to plot things into it.
Parameters
----------
width : str, default "100%"
The width of the Figure.
It may be a percentage or pixel value (like "300px").
height : str, default None
The height of the Figure.
It may be a percentage or a pixel value (like "300px").
ratio : str, default "60%"
A percentage defining the aspect ratio of the Figure.
It will be ignored if height is not None.
figsize : tuple of two int, default None
If you're a matplotlib addict, you can overwrite width and
height. Values will be converted into pixels in using 60 dpi.
self._template = Template(u"""
{{this.header.render(**kwargs)}}
{{this.html.render(**kwargs)}}
""")
# Create the meta tag.
self.header.add_children(Element(
''), # noqa
name='meta_http')
# Import Javascripts
for name, url in _default_js:
self.header.add_children(JavascriptLink(url), name=name)
# Import Css
for name, url in _default_css:
self.header.add_children(CssLink(url), name=name)
self.header.add_children(Element("""
<style>
html, body {
width: 100%;</style>
if script is not None:
figure.script.add_children(Element(script(self, kwargs)),
name=self.get_name())
def _repr_html_(self, **kwargs):
"""Displays the Div in a Jupyter notebook."""
if self._parent is None:
self.add_to(Figure())
out = self._parent._repr_html_(**kwargs)
self._parent = None
else:
out = self._parent._repr_html_(**kwargs)
return out
class IFrame(Element):
"""Create a Figure object, to plot things into it.
Parameters
----------
html : str, default None
Eventual HTML code that you want to put in the frame.
width : str, default "100%"
The width of the Figure.
It may be a percentage or pixel value (like "300px").
height : str, default None
The height of the Figure.
It may be a percentage or a pixel value (like "300px").
ratio : str, default "60%"
A percentage defining the aspect ratio of the Figure.
It will be ignored if height is not None.
figsize : tuple of two int, default None
def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
super(Figure, self).__init__()
self._name = 'Figure'
self.header = Element()
self.html = Element()
self.script = Element()
self.header._parent = self
self.html._parent = self
self.script._parent = self
self.width = width
self.height = height
self.ratio = ratio
if figsize is not None:
self.width = str(60*figsize[0])+'px'
self.height = str(60*figsize[1])+'px'
self._template = Template(u"""
{{this.header.render(**kwargs)}}
close_file : bool, default True
Whether the file has to be closed after write.
"""
if isinstance(outfile, text_type) or isinstance(outfile, binary_type):
fid = open(outfile, 'wb')
else:
fid = outfile
root = self.get_root()
html = root.render(**kwargs)
fid.write(html.encode('utf8'))
if close_file:
fid.close()
class Link(Element):
"""An abstract class for embedding a link in the HTML."""
def get_code(self):
"""Opens the link and returns the response's content."""
if self.code is None:
self.code = urlopen(self.url).read()
return self.code
def to_dict(self, depth=-1, **kwargs):
"""Returns a dict representation of the object."""
out = super(Link, self).to_dict(depth=-1, **kwargs)
out['url'] = self.url
return out
class JavascriptLink(Link):
"""Create a JavascriptLink object based on a url.
'<div style="width:{width};">'
'<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">' # noqa
''
'</div></div>').format
iframe = iframe(html=html,
width=self.width,
ratio=self.ratio)
else:
iframe = ('').format
iframe = iframe(html=html, width=self.width, height=self.height)
return iframe
class MacroElement(Element):
"""This is a parent class for Elements defined by a macro template.
To compute your own element, all you have to do is:
* To inherit from this class
* Overwrite the '_name' attribute
* Overwrite the '_template' attribute with something of the form::
{% macro header(this, kwargs) %}
...
{% endmacro %}
{% macro html(this, kwargs) %}
...
{% endmacro %}
{% macro script(this, kwargs) %}