Skip to content

Commit feec1e0

Browse files
split into two simpler Python examples
Clarify instructions for inserting Plotly output into Jinja2 templates and handling figure height.
1 parent 09aa8ba commit feec1e0

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

doc/python/interactive-html-export.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ By default, the resulting HTML file is a fully self-contained HTML file which ca
5757

5858
### Inserting Plotly Output into HTML using a Jinja2 Template
5959

60-
You can insert Plotly output and text related to your data into HTML templates using Jinja2. Use `.to_html` to send the HTML to a Python string variable rather than using `write_html` to send the HTML to a disk file. Use the `full_html=False` option to output just the code necessary to add a figure to a template. We do not want to output a full HTML page, as the Jinja template will define the rest of the page's structure — for example, the page's `HTML` and `BODY` tags. First create an HTML template file containing a Jinja variable, `{{ fig }}`. We use Python to generate HTML that is the template file with the Jinja variable `{{ fig }}` replaced with our graphic `fig`. The Python shows the steps to specify the height of the graphic as a percentage of the height of the browser window and provides a much simpler option if you are comfortable with a fixed height figure.
60+
You can insert Plotly output and text related to your data into HTML templates using Jinja2. Use `.to_html` to send the HTML to a Python string variable rather than using `write_html` which sends the HTML to a disk file. Use the `full_html=False` option to output just the code necessary for a component of a larger webpage. The Jinja template will provide the page's `HTML` and `BODY` tags. First create an HTML template file containing a Jinja variable, `{{ fig }}`. The Python takes the template file, replaces the Jinja variable `{{ fig }}` with our graphic `fig`, and saves the resulting complete HTML. The first Python example produces a webpage with a fixed height graph; a second example program shows the additional steps to specify the height of the graphic as a percentage of the height of the browser window.
6161

6262
<!-- #region -->
6363

@@ -90,12 +90,29 @@ fig = px.bar(data_canada, x='year', y='pop')
9090
output_html_path=r"/path/to/output.html"
9191
input_template_path = r"/path/to/template.html"
9292

93-
# code block to set the vertical height as a percentage of the window height
94-
# if you are comfortable with a fixed height graph, substitute
95-
# plotly_jinja_data = {"fig":fig.to_html(full_html=False)}
96-
# for all the code up to the end of the responsive Plotly figure HTML Jinja dictionary population block
93+
plotly_jinja_data = {"fig":fig.to_html(full_html=False)}
94+
#consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above
95+
96+
with open(output_html_path, "w", encoding="utf-8") as output_file:
97+
with open(input_template_path) as template_file:
98+
j2_template = Template(template_file.read())
99+
output_file.write(j2_template.render(plotly_jinja_data))
100+
```
101+
102+
The next example allows the size of your graphs to change depending on the screen resolution and window size. It replaces the `plotly_jinja_data = {"fig":fig.to_html(full_html=False)}` line with a code block that sets autosize to True, height to None and responsive to True. It replaces the `<div>` at the beginning of the to_html output with a new div that specifies the desired height as a percentage of the window height. It uses the same Jinja template (above) as the prior example.
103+
104+
105+
```python
106+
import plotly.express as px
107+
from jinja2 import Template
108+
109+
data_canada = px.data.gapminder().query("country == 'Canada'")
110+
fig = px.bar(data_canada, x='year', y='pop')
111+
112+
output_html_path=r"/path/to/output.html"
113+
input_template_path = r"/path/to/template.html"
97114

98-
# we defer to the HTML and window size for the height by setting autosize to True, height to None and responsive to True
115+
# set the vertical height as a percentage of the window height by setting autosize to True, height to None and responsive to True
99116
fig.update_layout(autosize=True, height=None, )
100117
fig_html = fig.to_html(full_html=False, config=dict(responsive=True))
101118
#consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above

0 commit comments

Comments
 (0)