How do I find the width of an IPython cell? - python

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)

Related

pygments highlight print in jupyter

I would like to print a string with sql into my jupyter notebook like below
This was done manually with pygments, see
Here is what I tried so far
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from IPython.core.display import HTML, display
query = '''
SELECT
*
FROM
latest.tmp
'''
display(HTML(highlight(query, SqlLexer(), HtmlFormatter())))
In the output keywords are neither green nor bold. (I am using Jupyterlab.)
You need to also set HtmlFormatter(full=True)

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

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.

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)

Latex with IPython (IPython) error

While trying to use LaTeX with IPython, I used the following code:
from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))
from How to write LaTeX in IPython Notebook?.
But, I got the following error.
<IPython.core.display.Math at 0x269a030>
What does this error mean? When do I get it? How can I get rid of it?
That is not an error. Run it with ipython notebook and you will see your integral displayed.
It is not an error, it is the output representation. Simply run the code inside the IPython notebook:
from IPython.display import display, Math, Latex
display(Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'))

Categories

Resources