Skip to content

Commit

Permalink
Allow get_export_names to skip configuration check (#1471)
Browse files Browse the repository at this point in the history
* Allow get_export_names to skip configuration check

This introduces an environment variable named
NBCONVERT_DISABLE_CONFIG_EXPORTERS that will cause get_export_names to
return all entrypoints instead of checking the "enabled" status of
each one.

Closes: #1439

* Update nbconvert/exporters/tests/test_exporter.py

Co-authored-by: Matt Riedemann <mriedem.os@gmail.com>

Co-authored-by: Matt Riedemann <mriedem.os@gmail.com>
  • Loading branch information
rmoe and mriedem authored Apr 26, 2021
1 parent 6f1a56f commit a365a5c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/autogen_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
CLI Flags and Aliases
---------------------
The dynamic loading of exporters can be disabled by setting the environment
variable ``NBCONVERT_DISABLE_CONFIG_EXPORTERS``. This causes all exporters
to be loaded regardless of the value of their ``enabled`` attribute.
When using Nbconvert from the command line, a number of aliases and flags are
defined as shortcuts to configuration options for convience.
Expand Down
5 changes: 5 additions & 0 deletions nbconvert/exporters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import os
import warnings

import entrypoints
Expand Down Expand Up @@ -135,6 +136,10 @@ def get_export_names(config=get_config()):
them as an nbconvert.exporter entrypoint.
"""
exporters = sorted(entrypoints.get_group_named('nbconvert.exporters'))
if os.environ.get("NBCONVERT_DISABLE_CONFIG_EXPORTERS"):
get_logger().info("Config exporter loading disabled, no additional exporters will be automatically included.")
return exporters

enabled_exporters = []
for exporter_name in exporters:
try:
Expand Down
31 changes: 30 additions & 1 deletion nbconvert/exporters/tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os
from traitlets.config import Config

from unittest.mock import patch

from .base import ExportersTestsBase
from ...preprocessors.base import Preprocessor
from ..exporter import Exporter
Expand Down Expand Up @@ -73,3 +75,30 @@ def test_get_export_names_disable(self):
})
export_names = get_export_names(config=config)
self.assertEqual(export_names, ['notebook'])

def test_get_exporter_disable_config_exporters(self):
"""
Does get_export_names behave correctly with respect to
NBCONVERT_DISABLE_CONFIG_EXPORTERS being set in the
environment?
"""
config = Config({
'Exporter': {'enabled': False},
'NotebookExporter': {'enabled': True}
})
os.environ["NBCONVERT_DISABLE_CONFIG_EXPORTERS"] = "1"
with patch("nbconvert.exporters.base.get_exporter") as exp:
export_names = get_export_names(config=config)
# get_export_names should not call get_exporter for
# any of the entry points because we return before then.
exp.assert_not_called()

# We should have all exporters, not just the ones
# enabled in the config
self.assertNotEqual(export_names, ['notebook'])

# In the absence of this variable we should revert to
# the normal behavior.
del os.environ["NBCONVERT_DISABLE_CONFIG_EXPORTERS"]
export_names = get_export_names(config=config)
self.assertEqual(export_names, ['notebook'])

0 comments on commit a365a5c

Please sign in to comment.