Skip to content

Commit e4d7b77

Browse files
authored
Merge pull request #5590 from rl-utility-man/patch-22
show how to make the output in the Jinja2 example responsive
2 parents 4cfec3d + 7dc4ed8 commit e4d7b77

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

doc/python/interactive-html-export.md

Lines changed: 33 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 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 template will define the rest of the page's structure — for example, the page's `HTML` and `BODY` tags. First create a template file containing HTML and a Jinja variable, `{{ fig }}`. We use Python to replace the Jinja variable with our graphic `fig`.
6161

6262
<!-- #region -->
6363

@@ -93,6 +93,34 @@ input_template_path = r"/path/to/template.html"
9393
plotly_jinja_data = {"fig":fig.to_html(full_html=False)}
9494
#consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above
9595

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 height of the figure can be made responsive by setting `autosize` to `True` and `height` to `None` in the layout, and `responsive` to `True` in the config.
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"
114+
115+
fig.update_layout(autosize=True, height=None)
116+
fig_html = fig.to_html(
117+
config={"responsive": True},
118+
default_height="80vh",
119+
full_html=False,
120+
)
121+
plotly_jinja_data = {"fig": fig_html}
122+
#consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above
123+
96124
with open(output_html_path, "w", encoding="utf-8") as output_file:
97125
with open(input_template_path) as template_file:
98126
j2_template = Template(template_file.read())
@@ -117,10 +145,9 @@ IFrame(snippet_url + 'interactive-html-export', width='100%', height=1200)
117145
<u><a href="https://go.plotly.com/dash-club?utm_source=Dash+Club+2022&utm_medium=graphing_libraries&utm_content=inline">Join now</a></u>.</p></div></div></div></div>
118146

119147

120-
### Full Parameter Documentation
148+
### Reference
121149

122-
```python
123-
import plotly.graph_objects as go
150+
See the following links for parameter information and HTML figure export options:
124151

125-
help(go.Figure.write_html)
126-
```
152+
- [`plotly.io.to_html()`](https://plotly.com/python-api-reference/generated/plotly.io.to_html.html)
153+
- [`plotly.io.write_html()`](https://plotly.com/python-api-reference/generated/plotly.io.write_html.html)

0 commit comments

Comments
 (0)