Nbconvert: How to execute notebook with the custom arguments?

Created on 23 Sep 2017  路  3Comments  路  Source: jupyter/nbconvert

I want to execute notebook by running this command:

jupyter nbconvert --to notebook --execute mynotebook.ipynb

How to give some arguments to .ipynb file?

I found traitlets and tried to define Configurable Class in the first cell of ipynb. But I couldn't reach my goal. (Because, maybe, arguments of the command were not reached to ipynb.)

I ideaed another method to use environment variables. Are there better methods?

Most helpful comment

Another approach for this variety of thing can be found at https://github.com/nteract/papermill

However, if you really wanted to do this in the way you suggested, I can think of two ways using code patterns and nbconvert:

  1. environment variables + os.environ
    You could use environment variables and in your notebook write your code so that it calls os.environ.get(your_env_variable_name, default_value_for_env_variable). Then you just need to set the environment variables whenever executing the notebook you're good to go (and your notebook is manageable even if you don't have those environment variables set because you have a default)

  2. custom preprocessors + traitlets
    You could create a custom preprocessor that looks for particular strings that you can guarantee to be unique across your cells and replaces that string with the parameterised value. You can define that parameterised value via a traitlet defined in your preprocessor. Then you can use the standard traitlets configuration options to change the final value that the notebook is run with.

    NB: I think you would need to guarantee that this processor is applied before the Execute preprocessor and off the top of my head I'm not sure if that happens if you add a custom preprocessor via a command line argument. If that is the case it may be easier to create a custom exporter that wraps your custom preprocessor that additionally guarantees that your preprocessor is applied prior to the execute preprocessor.

All 3 comments

Notebooks aren't really meant to take command line arguments like that. If you want to have a notebook with inputs, have a look at nbparameterise.

Another approach for this variety of thing can be found at https://github.com/nteract/papermill

However, if you really wanted to do this in the way you suggested, I can think of two ways using code patterns and nbconvert:

  1. environment variables + os.environ
    You could use environment variables and in your notebook write your code so that it calls os.environ.get(your_env_variable_name, default_value_for_env_variable). Then you just need to set the environment variables whenever executing the notebook you're good to go (and your notebook is manageable even if you don't have those environment variables set because you have a default)

  2. custom preprocessors + traitlets
    You could create a custom preprocessor that looks for particular strings that you can guarantee to be unique across your cells and replaces that string with the parameterised value. You can define that parameterised value via a traitlet defined in your preprocessor. Then you can use the standard traitlets configuration options to change the final value that the notebook is run with.

    NB: I think you would need to guarantee that this processor is applied before the Execute preprocessor and off the top of my head I'm not sure if that happens if you add a custom preprocessor via a command line argument. If that is the case it may be easier to create a custom exporter that wraps your custom preprocessor that additionally guarantees that your preprocessor is applied prior to the execute preprocessor.

Closing, as this isn't really about nbconvert, though I'm happy to reopen if you have particular questions.

Was this page helpful?
0 / 5 - 0 ratings