Use the tdnb.run_notebook() function to run another Jupyter notebook by opening another Jupyter session.
For example, you are running notebook with name notebook1.ipynb. After a successful connection to another system, you want to log a message.
Normally, the workflow for notebook1.ipynb is to keep a logger after establishing the connection. However, the ideal way is to define a logger at some other place and consume the logger in notebook1.ipynb. You can use tdnb.run_notebook() to run another notebook from the current notebook. So, in this scenario, you can define another notebook such as notebook_logger.ipynb, keep all logging related code here, and run notebook_logger.ipynb from notebook1.ipynb. Code is modularized using this scenario by moving the logging to another notebook notebook_logger.ipynb; notebook_logger.ipynb can now be run by any notebook which requires logging.
While running another notebook from the current notebook, you can return a message from another notebook to current notebook so the current notebook can decide whether another notebook execution is done as expected or not. Consider the previous scenario. If notebook_logger.ipynb returns a message and sent it to notebook1.ipynb, then notebook1.ipynb decides whether execution of notebook_logger.ipynb is successful or not. You can return a message from another notebook using tdnb.exit().
Required Parameter
- path
- Specifies the path of the notebook to run.
Optional Parameters
- timeout_seconds
- Specifies the timeout period in seconds to run the notebook.
- The timeout period is for a single cell and not for the entire notebook.
- If a timeout is not specified, then every cell will be executed until the corresponding operation completes.
- arguments
- Specifies the arguments to be used in place of widgets.
Usage Notes
As tdnb.run_notebook() executes a second notebook, you can pass parameters to another notebook for execution. Rules for passing arguments to another notebook follow.
- Parametize other notebook with either tdnb.text() or tdnb.dropdown() or tdnb.multi_select().
- Use tdnb.get() with the other notebook to refer the values, then pass the parameters to arguments from the current notebook.
- arguments accepts a dictionary. Keep the key as name of the widget used in another notebook and values as value to be sent to another notebook. For example, consider the logger scenario:
- logger_notebook.ipynb can have a text widget created by tdnb.text() with a name log_message.
- During logging, logger_notebook.ipynb can retrieve the message using tdnb.get('log_message') and logs it.
- The actual notebook, notebook1.ipynb, can send parameter to logger_notebook.ipynb:
tdnb.run_notebook('logger_notebook.ipynb', arguments={'log_message': 'log from notebook1.ipynb'}) - Observe that logger_notebook.ipynb is parameterized first by creating a text widget with name log_message. Since it is parameterized, notebook1.ipynb sent a value to the parameter log_message.
Example
Create two notebooks logger_notebook.ipynb and notebook1.ipynb. Parameterize notebook logger_notebook.ipynb using a text widget and log a message by retrieving the parameter from text widget. Then run this notebook from notebook1.ipynb by passing a parameter.
content of logger_notebook.ipynb
import logging
logger = logging.getLogger('LoggerNotebook')
logging.getLogger().setLevel(logging.INFO)
from teradatamlwidgets import tdnb
tdnb.text('log message', 'message', 'LogMessage:'
logger.warning(tdnb.get('log_message'))
content of notebook1.ipynb
from teradatamlwidgets import tdnb
tdnb.run_notebook('logger_notbook.ipynb', arguments={'log_message': 'Message1'})