Skip to content

Commit 09aa8ba

Browse files
show how to make the output in the Jinja2 example responsive
Updated instructions for inserting Plotly output into Jinja2 templates, including making the figure height responsive to the HTML and window height. Let me know if this should also be a bug report. This is related to the (cryptic) plotly/plotly.js#5270 and to https://community.plotly.com/t/plot-sizing-problems/1620/34
1 parent 4cfec3d commit 09aa8ba

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

doc/python/interactive-html-export.md

Lines changed: 16 additions & 2 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 don't want to output a full HTML page, as the 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 }}`. In this example, we customize the HTML in the template file by replacing the Jinja variable `{{ fig }}` with our graphic `fig`.
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.
6161

6262
<!-- #region -->
6363

@@ -90,7 +90,21 @@ 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-
plotly_jinja_data = {"fig":fig.to_html(full_html=False)}
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
97+
98+
# we defer to the HTML and window size for the height by setting autosize to True, height to None and responsive to True
99+
fig.update_layout(autosize=True, height=None, )
100+
fig_html = fig.to_html(full_html=False, config=dict(responsive=True))
101+
#consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above
102+
103+
vertical_height_as_pct_window = 80
104+
fig_html_with_vertical_height = f'<div style="height: {vertical_height_as_pct_window}vh;">'+fig_html.replace("<div>","", 1)
105+
plotly_jinja_data = {"fig":fig_html_with_vertical_height}
106+
# end of responsive Plotly figure HTML Jinja dictionary population block
107+
94108
#consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above
95109

96110
with open(output_html_path, "w", encoding="utf-8") as output_file:

0 commit comments

Comments
 (0)