I am using Jupyter on VS code, the graphs work fine when views on VS code, however when I upload the ipynb file on GitHub none of the graphs show up
Code:
import plotly.express as px
countryc = px.histogram(country_count , "Country" , color='type').update_xaxes(categoryorder = "total descending")
countryc.show()
I tried saving the the graph as an html file using :
countryc.write_html('/Python/Netflix EDA/countryc.html')
HTML(filename='/Python/Netflix EDA/countryc.html')
IFrame(src='/Python/Netflix EDA/countryc.html', width = 1300, height=700)
however I am still unable display this file when viewing on GitHub. Any suggestion on how to show the interactive files on GitHub?
Related
I am using gmplot in Python 3.8 to generate a layer of polygons on top of a satellite google maps layer. The map is saved to .html format but I would like to be able to convert the .html file to .png format to embed it in a pdf created in Python at a later stage (that will contain other elements, such as text and other images).
I generate the map using standard code as described in the gmplot tutorial:
import gmplot
latitude_list = [ 17.4567417, 17.5587901, 17.6245545]
longitude_list = [ 78.2913637, 78.007699, 77.9266135 ]
gmap = gmplot.GoogleMapPlotter(17.438139, 78.3936413, 11)
gmap.polygon(latitude_list, longitude_list, color = 'cornflowerblue')
gmap.draw("path_to_html")
I have checked different posts to get a solution, including this one and this one. From one of these posts, I have managed to get the following snippet of code:
import time
from selenium import webdriver
import chromedriver_binary # adds chromedriver binary to path
driver = webdriver.Chrome()
driver.get("local_url_of_html_file")
time.sleep(3)
driver.save_screenshot('map.png')
driver.quit()
It appears this code takes a screenshot of the html but I was wondering if there is any in-built function in gmplot to do this in a more straightforward way or other packages like bokeh.
I'm converting images to icons using this code:
import PIL.image
img = PIL.Image.open ("imagepath.png")
img.save ("iconpath.ico")
This is giving me an icon file as desired, but when I try to open it an error pops up:
Paint:
Microsoft photos error:
When I try to open other icons with the same programs they work perfectly, but it doesn't with the ones I made. Does anyone know any other way or library for doing this?
Try this:
img.save('iconpath.ico',format = 'ICO', sizes=[(32,32)])
You can change size to 16,16
First time I was converting image with PIL I've used this tutorial:
Tutorial
Everything worked fine.
The image that is being converted has to have a 1:1 proportion, if not, when trying to open the generated icon it will cause errors.
I am using altair on a jupyter notebook where I am trying to convert a pandas dataframe to a json file before passing it to Altair chart. I would like to save the final chart as a html file.
import altair as alt
alt.renderers.enable('notebook')
alt.data_transformers.enable('json')
from vega_datasets import data
url = 'data.json'
cars = data.cars()
cars.to_json(url, orient='records')
charts1=alt.Chart(url).mark_circle(size=60).encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Cylinders:O',
tooltip=['Name:Q', 'Origin:N', 'Horsepower:Q', 'Miles_per_Gallon:Q']
).interactive()
charts1.save('cars1.html')
charts2=alt.Chart(cars).mark_circle(size=60).encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Cylinders:O',
tooltip=['Name:Q', 'Origin:N', 'Horsepower:Q', 'Miles_per_Gallon:Q']
).interactive()
charts2.save('cars2.html')
charts1 | charts2
Both charts1 and charts2 are displayed correctly in jupyter notebook but only cars2.html is displayed properly when I open it in my browser. cars1.html is just an empty white box. Have I got the syntax wrong? I am in python3.6, Altair version: '2.2.2', Jupyter: '4.3.0'.
For chart1, the data exists in a separate file (data.json), and that file has to be accessible to the browser in order for the browser to render the data in a chart. If the data file is not accessible (either because it is at the wrong URL, or due to a cross-origin issue) then the result will be a blank chart.
To determine what the issue is, open your browser's javascript console and check what the error is.
For example, if you're viewing your chart from a file:// URL, the data may not be accessible if your browser has strict cross-origin policies. Try viewing the file via a local webserver instead (you can use python -m http.server).
I am using Plotly to make graphs in my IPython notebook. I am able to view graphs on my IPython notebook when I upload them on GitHub they are displayed as blank spaces.
I read on the web that Plotly currently does not support iframes and hence the issue, but is there a workaround?
Here's the link to my GitHub Ipython notebook:
https://github.com/dhavalbhinde/bhinde_dhaval_spring2017/blob/master/Finals/Analysis%203.ipynb
Please, can someone advice how should I handle them?
I found a way to show Plotly plots on Github. They aren’t interactive anymore but it’s better than nothing.
First
import plotly.io as pio
pio.renderers
you can see the list of available renders.
*if you get an error on this step you can simply just install orca:
conda install -c plotly plotly-orca
and then there are 2 possible ways.
you can pass "svg" to .show() like this:
fig = px.scatter_3d(iris, x=transformed_iris['component1'], y=transformed_iris['component2'], z=transformed_iris['component3'],color='species')
fig.show(renderer="svg")
or you can set the pio.renderers.default to svg:
pio.renderers.default = "svg"
Import these and this and it will work :
from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)
I have a jupyter notebook with interractive plotly plots. I am converting that notebook into slides using nbconvert. When I do so the plotly plots do not show up in the slides. I get the following tornado warnings as well
$ jupyter nbconvert presentation.ipynb --to slides --post serve
[NbConvertApp] Converting notebook presentation.ipynb to slides
[NbConvertApp] Writing 818538 bytes to presentation.slides.html
[NbConvertApp] Redirecting reveal.js requests to https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.1.0
Serving your slides at http://127.0.0.1:8000/presentation.slides.html
Use Control-C to stop this server
WARNING:tornado.access:404 GET /custom.css (127.0.0.1) 1.53ms
WARNING:tornado.access:404 GET /custom.css (127.0.0.1) 0.96ms
WARNING:tornado.access:404 GET /plotly.js (127.0.0.1) 0.84ms
To add insult to injury this worked yesterday and I don't think I changed anything substantial. I tried rebooting my browser and my machine and neither helped.
1) Check the JS console for errors and the Jupyter log if you are serving the slides via Jupyter. When you browse the slides.html, you may be getting
404 GET /files/mydir/plotly.js
put the plotly.js file in the directory where the slides.html is located (download e.g. https://cdn.plot.ly/plotly-latest.min.js and rename to plotly.js)
2) make sure you are specifying a Layout height and width in your Jupyter notebook e.g.
trace_data = [trace1]
layout = Layout(
autosize=False,
width=720,
height=480,
margin=Margin(
l=50,
r=50,
b=100,
t=100,
pad=4
),
bargroupgap=0.3
)
fig = Figure(data=trace_data, layout=layout)
Re-run your charts, check they appear properly in the notebook, save the notebook, re-run nbconvert.
You do not need to customize the custom.css and make a myreveal.tpl Reveal template and specify it on the nbconvert command line, but you can do so if you wish to customize your slides.
Following the code found here : https://nbviewer.jupyter.org/format/slides/github/tarokiritani/testjupyter/blob/master/test%20plotly.ipynb#/
I have found adding plotly.offline.init_notebook_mode(connected=True) into the same cell as the plotly.offline.(i)plot function works
You must have a plotly.js file in your directory where you are performing nbconvert. For some reason, "to html" will embed the plotly javascript into the HTML file, but "to slides" searches for a plotly.js file in the directory.
That said, you will have to template reveal.js to change dimensions of the slides depending on the size of your plot charts. That, or customize the dimension of the plotly graphs. If the slide are too big (or the other way around), the graphs will collapse into one line in the slides.