Zeppelin & spark.ipyspark & folium - python

I really like how folium works with python on jupyter notebooks (I haven't tried it, but judging from the tutorials). What I want to achieve is same functionality, but with zeppelin notebooks using spark.ipyspark. Folium functionality would be huge improvement of data plotting capabilities of zeppelin's notebooks.
What I tried is simple:
import folium
m = folium.Map(location=[45.5236, -122.6750])
m
This is only returning <folium.folium.Map at 0x10f4a3518>
What I tried next is to build HTML map, save it locally and then invoke it as output of zeppelin paragraph.
import folium
from IPython.display import HTML
from IPython.display import IFrame
m =folium.Map(
location=[45.5236, -122.6750],
tiles='Stamen Toner',
zoom_start=13
)
m.render_iframe = True
m.save('/Users/abc/m.html')
HTML("<iframe src=file:///Users/abc/m.html width=700 height=350></iframe>")
Which again gave me:
<IPython.core.display.HTML object>
Then I exchanged last row with:
IFrame("src=file:///Users/abc/m.html", width=700, height=350)
Which again:
<IPython.lib.display.IFrame at 0x112882c88>
When I try python's print using:
print("%html <iframe src=file:///Users/abc/m.html width=700, height=350></iframe>")
I get 700x350 blank white window as output of the paragraph. When I try to change src to for example "https://zeppelin.apache.org/" it works well.
I feel like two things are not working properly.
1. Folium module with zeppelin notebook which is not invoking map properly.
2. Showing local HTML page as output of zeppelin paragraph.
Does anybody tried this already? Was anybody successful to overcome this?

Thanks for advice, I was able to run it by adding:
html_string = m.get_root().render()
print("%html", html_string)
So now entire code looks like:
import folium
m =folium.Map(
location=[45.5236, -122.6750],
tiles='Stamen Toner',
zoom_start=13,
width=600,height=300
)
html_string = m.get_root().render()
print("%html", html_string)
EDIT:
using above described way was modifying appearance of entire zeppelin notebook. I used different method, using html_string = m._repr_html_(),which is according this link (github.com/python-visualization/folium/issues/781) used in jupyter for showing HTML in iframe.
So code now:
import folium
m =folium.Map(
location=[45.5236, -122.6750],
tiles='Stamen Toner',
zoom_start=13,
width=600,height=300
)
html_string = m._repr_html_()
print("%html", html_string)

Related

Execute Folium Library code in RMarkdown chunk doesn´t show up map

I`m working with RMarkdown and Python, I just created a chunk where I add code relate to the folium library, but I can create the report or show up the map
Chunk setup in my RMarkdown Report, I`m al so working with matplotlib in the same RMarkdown report a nd it works fine
```{r setup,include=FALSE}
library(reticulate)
library(ggplot2)
library(lattice)
library(vembedr)
use_python("C:/Users/renzocrossi/AppData/Local/Programs/Python/Python310/python.exe")
knitr::opts_chunk$set(echo=TRUE,message = FALSE,warning = FALSE)
This is the code in the RMarkdown chunk about folium
```{python}
import folium
m = folium.Map(location = [46.20, 6.144], zoom_start=6, tiles="OpenStreetMap")
fg = folium.FeatureGroup(name="My Map")
fg.add_child(folium.Marker(location=[40.12, 10.1], popup="Hi I'am a Marker",
icon=folium.Icon(color='green')))
m.add_child(fg)
m
```
<folium.folium.Map object at 0x000001F6A1098B20>
I ran the code in Python and didn't get an output either. The only way that I think this would work as-is is in a Jupyter notebook. (I think!)
However, with just a bit more, you can definitely get the map on the document. I added import webbrowswer so that I could export the map as HTML, then render it in R Markdown.
```{python mapper, include=FALSE}
import folium
import webbrowser
m = folium.Map(location = [46.20, 6.144], zoom_start=6, tiles="OpenStreetMap")
fg = folium.FeatureGroup(name="My Map")
fg.add_child(folium.Marker(location=[40.12, 10.1], popup="Hi I'am a Marker",
icon = folium.Icon(color='green')))
m.add_child(fg)
m.save("map.html")
webbrowser.open("map.html")
```
Then outside of the Python chunk (not in any chunk), I called the map.
![](map.html)
![](map.html){width=100% height=800px}
I don't know if you have any default figure sizes set, I didn't for this example. However, the calls of this map rendered this:
By the way, the maps are still completely interactive, as they would be in Python or if rendered directly through R.
For example, I decreased magnification and rendered the following:

How do I find the width of an IPython cell?

pandas dataframes are displayed nicely within the ipython cell. How does it do it?
The regular ways of getting the console width for Python do not seem to work for ipython cells.
If you just want to see what is the size you can use this script:
from IPython.display import display, HTML
js = """<script>
alert($( ".cell").width())
</script>"""
display(HTML(js))
If you want to use in code you can assign it to a variable and use it in next cell:
from IPython.display import display, HTML
js = """<script>
IPython.notebook.kernel.execute("cell_width="+($( ".cell").width()))
</script>"""
display(HTML(js))
In the next cell:
print(cell_width)

displaying maps from normal text editor instead of notebook

I want to create world maps using folium within a text editor (geany), not using notebooks. this piece of code will work but i cant see the output. i.e, the actual map. How do i get it to display the map.
import pandas as pd
import numpy as np
import folium
m = folium.Map(location=[40.0150, -105.2705])
# Display the map
m
You can use import webbrowser and open the saved html file in your browser.
import folium
import webbrowser
m = folium.Map(location=[40.0150, -105.2705])
m.save("map.html")
# Display the map
webbrowser.open("map.html")
Just like yabberth said save the map as map.html after saving html call the system command like os.system('map.html')

Display TomTom map with folium

I am starting data scientist and I am doing a benchmark for maps.
I would like to visualize the TomTom map API in Jupyter notebook with folium to compare it with OpenStreetMap. Openstreet map is supported by folium so that is easy. This code is doing the trick:
import folium
OSM_map = folium.Map(location=[45.523, -122.675],
zoom_start=13,
tiles="OpenStreetMap")
Now, I would like to do the same with the TomTom maps API. On developer.tomtom.com I find that this is the request URL:
https://api.tomtom.com/map/1/tile/basic/main/0/0/0.png?view=Unified&key=*****
So I thought to implement this in folium. I don't get an error message but it is just displaying a gray map.
TomTom_map = folium.Map(
location=[45.523, -122.675],
zoom_start=10,
tiles='http://{s}.api.tomtom.com/map/1/tile/basic/main/{z}/{x}/{y}.png',
API_key = 'xxxxxx',
attr='TomTom')
I follow literally the example of the folium documentation but it does not work. Anyone knows how to solve this? That would be great :). Cheers.
Thanks Bob and szogoon,
It works now! I replaced the code with:
import folium
TomTom_map = folium.Map(
location=[45.523, -122.675],
zoom_start=10,
tiles= 'http://{s}.api.tomtom.com/map/1/tile/basic/main/{z}/{x}/{y}.png?
view=Unified&key=********',
attr='TomTom')

the Bokeh plot not show up on chrome

I had code on jupyter notebook:
from bokeh.plotting import figure
from bokeh.io import output_file, show
x = [1,2,3,4,5]
y = [6,7,8,9,10]
output_file("Line.htlm")
f = figure()
f.line(x,y)
show(f)
No Error occured but i recieved this on chorme:
enter image description here
But when try the html result file with firefox, it was worked:
enter image description here
Could someone teach me how to fix the problem one chrome
That is the Bokeh HTML output. But you have mis-named your output file .htlm instead of .html so chrome has just opened the output as plain text. Also, if you you would like to see output inline in the notebook itself, instead of a new tab, use output_notebook.

Categories

Resources