-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
show how to make the output in the Jinja2 example responsive #5590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rl-utility-man
wants to merge
1
commit into
plotly:doc-prod
Choose a base branch
from
rl-utility-man:patch-22
base: doc-prod
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,7 @@ By default, the resulting HTML file is a fully self-contained HTML file which ca | |
|
|
||
| ### Inserting Plotly Output into HTML using a Jinja2 Template | ||
|
|
||
| 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`. | ||
| 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. | ||
|
|
||
| <!-- #region --> | ||
|
|
||
|
|
@@ -90,7 +90,21 @@ fig = px.bar(data_canada, x='year', y='pop') | |
| output_html_path=r"/path/to/output.html" | ||
| input_template_path = r"/path/to/template.html" | ||
|
|
||
| plotly_jinja_data = {"fig":fig.to_html(full_html=False)} | ||
| # code block to set the vertical height as a percentage of the window height | ||
| # if you are comfortable with a fixed height graph, substitute | ||
| # plotly_jinja_data = {"fig":fig.to_html(full_html=False)} | ||
| # for all the code up to the end of the responsive Plotly figure HTML Jinja dictionary population block | ||
|
|
||
| # we defer to the HTML and window size for the height by setting autosize to True, height to None and responsive to True | ||
| fig.update_layout(autosize=True, height=None, ) | ||
| fig_html = fig.to_html(full_html=False, config=dict(responsive=True)) | ||
| #consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above | ||
|
|
||
| vertical_height_as_pct_window = 80 | ||
| fig_html_with_vertical_height = f'<div style="height: {vertical_height_as_pct_window}vh;">'+fig_html.replace("<div>","", 1) | ||
| plotly_jinja_data = {"fig":fig_html_with_vertical_height} | ||
| # end of responsive Plotly figure HTML Jinja dictionary population block | ||
|
Comment on lines
+93
to
+106
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make more sense to add your code as a separate example. I'd like to keep the initial example simple. Could you please add a new example in this section with your code and provide a description above it? |
||
|
|
||
| #consider also defining the include_plotlyjs parameter to point to an external Plotly.js as described above | ||
|
|
||
| with open(output_html_path, "w", encoding="utf-8") as output_file: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make it clear that that the "Jinja template" and the "HTML template" are the same thing. Per my suggestion below, I think the description of the example that you're adding should appear above that example, so I removed it here.