You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is straight forward to create each potential view as a separate graph and then use Jinja to insert each potential view into a div on a JavaScript enabled webpage with a dropdown that chooses which div to display. This approach produces code that requires little customization or updating as you e.g. add, drop, or reorder views or traces, so it is particularly compelling for prototyping and rapid iteration. It produces web pages that are larger than the webpages produced through the built in method which is a consideration for very large figures with hundreds or thousands of data points in traces that appear in multiple selections. This approach requires both a Python program and a Jinja template file. The documentation on [using Jinja templates with Plotly](https://plotly.com/python/interactive-html-export/#inserting-plotly-output-into-html-using-a-jinja2-template) is relevant background.
450
+
451
+
<!-- #region -->
452
+
453
+
#### Python Code File
454
+
455
+
```python
456
+
import plotly.express as px
457
+
from jinja2 import Template
458
+
import collections
459
+
# Load the gapminder dataset
460
+
df = px.data.gapminder()
461
+
462
+
# Create a dictionary with Plotly figures as values
463
+
fig_dict = {}
464
+
465
+
# we need to fill that dictionary with figures. this example assumes that each figure has a title and that
466
+
# we want to use the titles as descriptions in the drop down
467
+
# This example happens to fill the dictionary by creating a scatter plot for each continent using the 2007 Gapminder data
labels={'gdpPercap': 'GDP per Capita (USD)', 'lifeExp': 'Life Expectancy (Years)'},
475
+
hover_name='country',size="pop", size_max=55
476
+
)
477
+
#Standardizing the axes makes the graphs easier to compare
478
+
fig_dict[continent].update_xaxes(range=[0,50000])
479
+
fig_dict[continent].update_yaxes(range=[25,90])
480
+
481
+
482
+
# Create a dictionary, data_for_jinja with two entries:
483
+
# the value for the "dropdown_entries" key is a string containing a series of <option> tags, one tag for each item in the drop down
484
+
# the value for the "divs" key is a string with a series of <div> tags, each containing the content that appears only when the user selects the corresponding item from the dropdown
485
+
# in this example, the content of each div is a figure and descriptive text.
486
+
data_for_jinja= collections.defaultdict(str)
487
+
text_dict = {}
488
+
for n, figname inenumerate(fig_dict.keys()):
489
+
text_dict[figname]=f"Here is some custom text about the {figname} figure"#This is a succinct way to populate text_dict; in practice you'd probably populate it manually elsewhere
0 commit comments