Dynamic Book Chapter Creation #7694
-
DescriptionI have a situation where I’d like to dynamically create chapters for a Quarto HTML book. In the sample code below, I’m producing results for five different segments using a loop. I’d like each iteration to produce a new book chapter. I can produce the desired book results by creating one notebook for each segment, but that’s not ideal since any changes need to be made in each notebook. Again using the sample code below, I'd like to produce a new chapter for each of the five segments (A-E). I’m using Jupyter Lab, Python, and Quarto version 1.2.475 in a Windows environment. # Set Up
import numpy as np
import pandas as pd
import plotly.express as px # Make Data
np.random.seed(42)
n_observations = 1000
df = pd.DataFrame({
'segment': np.random.choice(['A', 'B', 'C', 'D', 'E'], n_observations),
'var1': np.random.rand(n_observations),
'var2': np.random.rand(n_observations)
}) # Results Across All Segments. Ideally, this content would be in a chapter.
segment_results = df.groupby('segment').apply(lambda x: pd.Series({
'# of observations': (x['segment'].count()),
'var1 mean': (x['var1'].mean()),
'var2 mean': (x['var2'].mean()),
'correlation': (np.corrcoef(x['var1'], x['var2'])[0, 1])
}))
total_row = pd.Series({
'# of observations': df['segment'].count(),
'var1 mean': df['var1'].mean(),
'var2 mean': df['var2'].mean(),
'correlation': np.corrcoef(df['var1'], df['var2'])[0, 1]
}, name='Total')
total_row = pd.DataFrame(total_row).transpose()
results = pd.concat([segment_results, total_row]).rename_axis('Segment')
results['# of observations'] = results['# of observations'].map('{:,.0f}'.format)
display(results)
px.scatter(df.sort_values('segment'), x='var1', y='var2', color='segment', title='All Observations') # Results by Segment. Ideally, results for each segment would be in a separate chapter.
for segment in df['segment'].sort_values().unique():
print(f"Segment: {segment}")
print(results[results.index==segment].reset_index(drop=True).to_string(index=False))
display(px.scatter(df[df['segment']==segment], x='var1', y='var2', title=f"Segment {segment}")) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Use pre-render script and generate the chapter files. Also, I suggest you upgrade to 1.3, especially since 1.4 is on the horizon. |
Beta Was this translation helpful? Give feedback.
I think it is a good idea and something I will take a look at!
#1917
(I'll mark this discussion closed in favor of the above issue)