I have a plotly graph stored in a html file, file.html. This was created in another script using for example:
import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(y=[2, 3, 1]))
fig.write_html('file.html', auto_open=True)
Is there a way to open directly the html file without rewrite the plotly code?
Something like:
fig = go.read_html('file.html')
I need the plot inside the variable. For example this has to work:
fig = read html file file.html
fig.write_html('copyOfFile.html', auto_open=True)
I use both Python and R so I'd like a solution for both/one of them.
You can use python standard library webbrowser. It uses default browser to open the document.
import webbrowser
webbrowser.open('file.html')
Related
looking for help on how to load an altair chart from a json file (contains dict_keys(['$schema', 'config', 'datasets', 'title', 'vconcat'])).
The json file was created using the altair.Chart method to_json() such as below:
import altair as alt
chart = alt.Chart(df).mark_line(...).encode(...).properties(...).transform_filter(...)
chart_json = chart.to_json()
Here is a sample of the code I would like to run
chart = alt.load_json(chart_json) # made-up, needs replacement
chart.save('chart.png')
Disclaimer: I've never used altair and am trying to reverse-engineer a project. Thanks in advance for the help!
Altair can work directly with json files by specifying their path/url as shown in the documentation:
import altair as alt
from vega_datasets import data
url = data.cars.url # URL/path to json data
alt.Chart(url).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q'
)
To load a local file, there are several potential issues, including the browser having access to local files and using the correct format for the frontend you're using (e.g. jupyterlab). These have been elaborated on elsewhere:
Reference local .csv file in altair chart
https://github.com/altair-viz/altair/issues/2529#issuecomment-982643293
https://github.com/altair-viz/altair/issues/2432
It sounds like you're looking for the alt.Chart.from_json method:
new_chart = alt.Chart.from_json(chart_json)
new_chart.display()
The method assumes that chart_json is a string of JSON representing a valid Altair chart.
I try to run the below codes but I have a problem in showing the results.
also, I use pycharm IDE.
from fastai.text import *
data = pd.read_csv("data_elonmusk.csv", encoding='latin1')
data.head()
data = (TextList.from_df(data, cols='Tweet')
.split_by_rand_pct(0.1)
.label_for_lm()
.databunch(bs=48))
data.show_batch()
The output while I run the line "data.show_batch()" is:
IPython.core.display.HTML object
If you don't want to work within a Jupyter Notebook you can save data as an HTML file and open it in a browser.
with open("data.html", "w") as file:
file.write(data)
You can only render HTML in a browser and not in a Python console/editor environment.
Hence it works in Jupiter notebook, Jupyter Lab, etc.
At best you call .data to see HTML, but again it will not render.
I solved my problem by running the codes on Jupiter Notebook.
You could add this code after data.show_batch():
plt.show()
Another option besides writing it do a file is to use an HTML parser in Python to programatically edit the HTML. The most commonly used tool in Python is beautifulsoup. You can install it via
pip install beautifulsoup4
Then in your program you could do
from bs4 import BeautifulSoup
html_string = data.show_batch().data
soup = BeautifulSoup(html_string)
# do some manipulation to the parsed HTML object
# then do whatever else you want with the object
Just use the data component of HTML Object.
with open("data.html", "w") as file:
file.write(data.data)
I have created a local html plotly graph which I intend to run from a server, every time the script ends it opens the html, is there something I can use which will prevent this happening?
thanks
Yep! Just looking at script, find in the end line, where plotly draw a plot. It should look such this:
plotly.offline.plot(fig)
And add parameter auto_open and specify it to False:
plotly.offline.plot(fig, auto_open = False)
Hope it helps!
I can create an html file using this code:
with open(file_loc+'file.html', 'w') as html:
html.write(s.set_table_attributes("border=1").render())
How can I show the output in Jupyter Notebooks without creating the file?
If I simply try to render it in Jupyter (shown below) then it shows the html code instead of displaying the desired output that I would see in my browser:
from IPython.core.display import display, HTML
s.set_table_attributes("border=1").render()
Use this
from IPython.display import HTML
HTML(filename="profiling/z2pDecisionTreeProfiling.html")
You need to invoke IPython's HTML function:
HTML(s.set_table_attributes("border=1").render())
I am trying to import a csv file into Python but it doesn't seem to work unless I use the Import Data icon.
I've never used Python before so apologies is I am doing something obviously wrong. I use R and I am trying to replicate the same tasks I do in R in Python.
Here is some sample code:
import pandas as pd
import os as os
Main_Path = "C:/Users/fan0ia/Documents/Python_Files"
Area = "Pricing"
Project = "Elasticity"
Path = os.path.join(R_Files, Business_Area, Project)
os.chdir(Path)
#Read in the data
Seasons = pd.read_csv("seasons.csv")
Dep_Sec_Key = pd.read_csv("DepSecKey.csv")
These files import without any issues but when I execute the following:
UOM = pd.read_csv("FINAL_UOM.csv")
Nothing shows in the variable explorer panel and I get this in the IPython console:
In [3]: UOM = pd.read_csv("FINAL_UOM.csv")
If I use the Import Data icon and use the wizard selecting DataFrame on the preview tab it works fine.
The same file imports into R with the same kind of command so I don't know what I am doing wrong? Is there any way to see what code was generated by the wizard so I can compare it to mine?
Turns out the data had imported, it just wasn't showing in the variable explorer