Skip to content

Commit

Permalink
REF/ENH: Add templates for ipynb, update requirements and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
wrobstory committed Apr 22, 2014
1 parent 1e796e7 commit 381ab63
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 19 deletions.
25 changes: 22 additions & 3 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,25 @@
from jinja2 import Environment, PackageLoader
from pkg_resources import resource_string, resource_filename
import utilities
from uuid import uuid4


ENV = Environment(loader=PackageLoader('folium', 'templates'))


def initialize_notebook():
"""Initialize the IPython notebook display elements"""
try:
from IPython.core.display import display, HTML
except ImportError:
print("IPython Notebook could not be loaded.")

lib_js = ENV.get_template('ipynb_init_js.html')
lib_css = ENV.get_template('ipynb_init_css.html')

display(HTML(lib_js.render()))
display(HTML(lib_css.render()))

def iter_obj(type):
'''Decorator to keep count of different map object types in self.mk_cnt'''
def decorator(func):
Expand Down Expand Up @@ -92,6 +109,7 @@ def __init__(self, location=None, width=960, height=500,

#Init Map type
self.map_type = 'base'
self.map_id = '_'.join(['folium', uuid4().hex])

#Mark counter, JSON, Plugins
self.mark_cnt = {}
Expand All @@ -110,10 +128,11 @@ def __init__(self, location=None, width=960, height=500,
.format(width, height))

#Templates
self.env = Environment(loader=PackageLoader('folium', 'templates'))
self.env = ENV
self.template_vars = {'lat': location[0], 'lon': location[1],
'size': self._size, 'max_zoom': max_zoom,
'zoom_level': zoom_start}
'zoom_level': zoom_start,
'map_id': self.map_id}

#Tiles
self.tiles = ''.join(tiles.lower().strip().split())
Expand Down Expand Up @@ -189,7 +208,7 @@ def simple_marker(self, location=None, popup='Pop Text', popup_on=True):
add_mark))

@iter_obj('line')
def line(self, locations,
def line(self, locations,
line_color=None, line_opacity=None, line_weight=None):
'''Add a line to the map with optional styles.
Expand Down
26 changes: 13 additions & 13 deletions folium/templates/fol_template.html
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7/leaflet.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>

{{ dvf_js }}
{{ d3 }}
{{ vega }}
{{ jquery }}


<style>

#map {
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
}

</style>
</head>

<body>

<div id="map" {{ size }}></div>
<div id="{{ map_id }}" {{ size }}></div>

<script>

{{ vega_parse }}
var map = L.map('map').setView([{{ lat }}, {{ lon }}], {{ zoom_level }});

var map = L.map('{{ map_id }}').setView([{{ lat }}, {{ lon }}], {{ zoom_level }});

L.tileLayer('{{ Tiles }}', {
maxZoom: {{ max_zoom }},
attribution: '{{ attr }}'
}).addTo(map);

{% for mark, popup, add_mark in markers %}
{{ mark }}
{{ popup }}
{{ add_mark }}
{% endfor %}

{% for line, add_line in lines %}
{{ line }}
{{ add_line }}
{% endfor %}

{{ lat_lng_pop }}

{{ click_pop }}

</script>

</body>
37 changes: 37 additions & 0 deletions folium/templates/ipynb_init_js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script>

var folium_event = new CustomEvent(
"folium_libs_loaded",
{bubbles: true, cancelable: true}
);

var load_folium_charts = function(){
window.dispatchEvent(folium_event);
};

function load_folium_libs(){
console.log('Loading all Folium libraries...')
$.getScript("http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js", function(){
load_folium_charts()
})

if(typeof define === "function" && define.amd){
if (window['d3'] === undefined){
require.config(
{paths: {d3: 'http://d3js.org/d3.v3.min'}
}
);
require(["d3"], function(d3){
console.log('Loading from require.js...')
window.d3 = d3;
load_folium_libs();
});
} else {
load_folium_libs();
}
}else{
console.log('Require.js not found!');
throw "Require.js not found!"
};

</script>
44 changes: 44 additions & 0 deletions folium/templates/ipynb_repr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

<div id="{{ map_id }}" {{ size }}></div>

<script>

var make_plot = function(){
if (typeof L === 'undefined'){
window.addEventListener('leaflet_libs_loaded', make_plot)
return;
}

var render_plot = (function(){

{{ vega_parse }}

var map = L.map('{{ map_id }}').setView([{{ lat }}, {{ lon }}], {{ zoom_level }});

L.tileLayer('{{ Tiles }}', {
maxZoom: {{ max_zoom }},
attribution: '{{ attr }}'
}).addTo(map);

{% for mark, popup, add_mark in markers %}
{{ mark }}
{{ popup }}
{{ add_mark }}
{% endfor %}

{% for line, add_line in lines %}
{{ line }}
{{ add_line }}
{% endfor %}

{{ lat_lng_pop }}

{{ click_pop }}

})();

};

make_plot();

</script>
2 changes: 1 addition & 1 deletion folium/templates/jquery_ref.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
2 changes: 1 addition & 1 deletion folium/templates/vega_ref.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<script src="http://trifacta.github.com/vega/vega.js"></script>
<script src="http://trifacta.github.io/vega/vega.js"></script>
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Jinja2==2.6
Jinja2==2.7.2

0 comments on commit 381ab63

Please sign in to comment.