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
### Creating several independent graphs and using Jinja to insert them into a JavaScript enabled webpage
448
+
449
+
It is often easier 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 drop down 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. This requires both a Python program and a Jinja template file. You may want to consult the documentation on [using Jinja templates with Plotly](https://plotly.com/python/interactive-html-export/#inserting-plotly-output-into-html-using-a-jinja2-template).
450
+
451
+
#### Python Code File
452
+
453
+
```
454
+
import plotly.express as px
455
+
from jinja2 import Template
456
+
import collections
457
+
# Load the gapminder dataset
458
+
df = px.data.gapminder()
459
+
460
+
#create a dictionary with Plotly figures as values
461
+
fig_dict = {}
462
+
463
+
# Loop through each unique continent and create a scatter plot using the 2007 data
labels={'gdpPercap': 'GDP per Capita (USD)', 'lifeExp': 'Life Expectancy (Years)'},
471
+
hover_name='country',
472
+
)
473
+
474
+
# Create a dictionary, data_for_jinja with two entries:
475
+
# the value for the "dropdown_entries" key contains string containing a series of <option> tags, one for each item in the drop down
476
+
# the value for the "divs" key contains 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
477
+
# in this example, that content is a figure and descriptive text.
478
+
data_for_jinja= collections.defaultdict(str)
479
+
text_dict = {}
480
+
for n, figname in enumerate(fig_dict.keys()):
481
+
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