the Bokeh plot not show up on chrome - python

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.

Related

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')

Zeppelin & spark.ipyspark & folium

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)

prevent matplotlib output in jupyter notebook (python)

I tried to render several hundred images with python and matplotlib using the following code:
def render_state(np_data_array):
filename = "render/fig_" + str(i) + ".png"
fig = plt.figure(figsize = (12,12) )
aa = fig.add_subplot(111)
aa.imshow(np_data_array,cmap='gray')
fig.savefig(filename)
unfortunately matplotlib is also rendering everything into the jupyter notebook.
Is there a way to prevent any output from matplotlib? I couldn't really find a way and most answers on the internet just say to not use plt.show() what I don't even use
%%capture as the first line of the cell works like charm!
Thanks to ImportanceOfBeingErnest
You can also just use plt.close() at the end of your notebook cell like explained here. This way, you can still render other widgets like a tqdm progress bar. With %%capture, all widgets are blocked.

Plotly opens prompt screen after drawing plot

I use the following code to draw a heatmap in plotly:
import plotly.offline as plotly
import plotly.graph_objs as graph_objs
x = []
# fill x with stuff
path = os.path.join(self.get_current_job_directory(), track + '.html')
trace = graph_objs.Heatmap(z = x)
data = [trace]
plotly.plot(data, filename = path)
But I get a prompt screen like this. I need to generate hundreds of such plots on a remote server and its not practical to just dismiss them.
How to get rid of this?
Using the filename argument tells Plotly what filename to use for the HTML file it generates to contain the plot. That file is then viewed in the system's default HTML viewer, which in this case appears to be Lynx. Of course that's rather useless as the point is to view the plot, and Lynx is a text-only Web browser!
To avoid opening the plot, add auto_open=False to your plot() call:
plotly.plot(data, filename=path, auto_open=False)

Categories

Resources