Hi.
In this video we will learn how to parameterize the notebook and how to
run and export it from Python. In some case,
you may want to run the same notebook for multiple values of some
parameter, and you would like to have separate H T M L reports. For example,
you may want to execute same analysis on multiple segments of customers or
multiple countries. If you are working from the ui,
the solution for this is quite easy. You will set up in the notebook.
The best is at the beginning, the variables, uh,
which you want to use as parameters.
And then you execute and export the notebook as you change its values.
In the case, when you have just one or few parameters, it is fine,
but when you have more of them,
it is preferred to put the parameters in a separate file.
And then in the notebook, you just read the file and extract these parameters,
uh, from the file. YAML files, uh,
require a bit of formatting, uh, but it is very easy to use.
This method of parameters in a separate YAML file
is also preferred when you want to do some automation where you will not
execute and export the notebook from the ui,
but from the terminal or python code.
Now let's check our configuration,
the YAML file with the parameters and let's adapt our notebook accordingly.
Here is the config of the yamo file.
We can parameterize if to fit the intercept or not when fitting
the regression model and the alpha parameter in the chart.
Now let's adapt the notebook.
We have to import the yamo package.
Then we load the yamo config file.
We use the safe lot method from yaml.
Then we extract from the config these parameters.
After you load the config file to the config variable. Uh,
config is basically only a Python dictionary.
Okay? And now we have to change, uh, the variables here.
We
have to replace the value for alpha
and in the model, the fit intercept,
and we can create another section here. For example,
loading the config.
Okay,
now let's run it.
Okay, everything works.
Now imagine, uh,
that we would like to create reports for combination of two values for
fit intercept and five for alpha. That makes 10 reports.
We want to have the H T M L file and also the notebook for each.
That is altogether 20 files.
This is a lot of manual work because there are multiple steps
you have to change manually. The configuration file,
you have to execute the notebook, you have to export it,
and then you have to rename the notebook and start again.
We can create a simple script which automates execution, exporting,
and renaming step. It'll have just two parameters.
The configuration file path.
That means we can prepare multiple configuration files advanced before launching
the script and a name of newly created notebook.
I have created this script already, so let's see how it works.
Before we use it, we have to do one more thing in the notebook,
and that is to create a variable config path. In the first cell of the notebook,
we'll input the value for our configuration file,
and let's change it also Here,
save the notebook,
the file. What we'll be using is this one, execute, export, and B pi.
Let's run it.
This is the command item.
Then the name of our script parameter C uh,
stands for the configuration file path,
and the parameter n stands for the name of our newly created notebook.
Okay, it was down. Now let's explain that script
in the notebook we have in the first cell, and it has to be in the first cell,
both where we read the pass to configuration file.
This is how the NB parameterized library works. Uh,
this is the library which handles the change of parameters in the notebook.
For this library,
the only variables it can access and change are the variables in the first
cell of the notebook.
Then in the script, we input two parameters, pass to configuration file,
and the name of our newly created notebook.
This will be also the name of the exported version.
Then we read the notebook, uh, to a variable.
We extract its parameters
and we read and replace the parameter values.
Notice that config path is the variable name in the notebook
and path config is the parameter name of the script.
Then we execute the notebook,
then we save the executed notebook with the new name.
And lastly, we export the notebook to H T M L file with embedded, uh,
table of contents and pictures and charts.
In our next video, we will learn how to embed H T M L into our notebook.