base.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """Module containing single call export functions."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import os
  5. import sys
  6. if sys.version_info < (3, 10):
  7. from importlib_metadata import entry_points # type:ignore[import-not-found]
  8. else:
  9. from importlib.metadata import entry_points
  10. from nbformat import NotebookNode
  11. from traitlets.config import get_config
  12. from traitlets.log import get_logger
  13. from traitlets.utils.importstring import import_item
  14. from .exporter import Exporter
  15. # -----------------------------------------------------------------------------
  16. # Functions
  17. # -----------------------------------------------------------------------------
  18. __all__ = [
  19. "Exporter",
  20. "ExporterNameError",
  21. "export",
  22. "get_export_names",
  23. "get_exporter",
  24. ]
  25. class ExporterNameError(NameError):
  26. """An exporter name error."""
  27. class ExporterDisabledError(ValueError):
  28. """An exporter disabled error."""
  29. def export(exporter, nb, **kw):
  30. """
  31. Export a notebook object using specific exporter class.
  32. Parameters
  33. ----------
  34. exporter : ``Exporter`` class or instance
  35. Class or instance of the exporter that should be used. If the
  36. method initializes its own instance of the class, it is ASSUMED that
  37. the class type provided exposes a constructor (``__init__``) with the same
  38. signature as the base Exporter class.
  39. nb : :class:`~nbformat.NotebookNode`
  40. The notebook to export.
  41. config : config (optional, keyword arg)
  42. User configuration instance.
  43. resources : dict (optional, keyword arg)
  44. Resources used in the conversion process.
  45. Returns
  46. -------
  47. tuple
  48. output : str
  49. The resulting converted notebook.
  50. resources : dictionary
  51. Dictionary of resources used prior to and during the conversion
  52. process.
  53. """
  54. # Check arguments
  55. if exporter is None:
  56. msg = "Exporter is None"
  57. raise TypeError(msg)
  58. if not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
  59. msg = "exporter does not inherit from Exporter (base)"
  60. raise TypeError(msg)
  61. if nb is None:
  62. msg = "nb is None"
  63. raise TypeError(msg)
  64. # Create the exporter
  65. resources = kw.pop("resources", None)
  66. exporter_instance = exporter if isinstance(exporter, Exporter) else exporter(**kw)
  67. # Try to convert the notebook using the appropriate conversion function.
  68. if isinstance(nb, NotebookNode):
  69. output, resources = exporter_instance.from_notebook_node(nb, resources)
  70. elif isinstance(nb, (str,)):
  71. output, resources = exporter_instance.from_filename(nb, resources)
  72. else:
  73. output, resources = exporter_instance.from_file(nb, resources)
  74. return output, resources
  75. def get_exporter(name, config=None):
  76. """Given an exporter name or import path, return a class ready to be instantiated
  77. Raises ExporterName if exporter is not found or ExporterDisabledError if not enabled
  78. """
  79. if config is None:
  80. config = get_config()
  81. if name == "ipynb":
  82. name = "notebook"
  83. try:
  84. exporters = entry_points(group="nbconvert.exporters")
  85. items = [e for e in exporters if e.name == name or e.name == name.lower()]
  86. exporter = items[0].load()
  87. if getattr(exporter(config=config), "enabled", True):
  88. return exporter
  89. raise ExporterDisabledError('Exporter "%s" disabled in configuration' % (name))
  90. except IndexError:
  91. pass
  92. if "." in name:
  93. try:
  94. exporter = import_item(name)
  95. if getattr(exporter(config=config), "enabled", True):
  96. return exporter
  97. raise ExporterDisabledError('Exporter "%s" disabled in configuration' % (name))
  98. except ImportError:
  99. log = get_logger()
  100. log.error("Error importing %s", name, exc_info=True) # noqa: G201
  101. msg = 'Unknown exporter "{}", did you mean one of: {}?'.format(
  102. name, ", ".join(get_export_names())
  103. )
  104. raise ExporterNameError(msg)
  105. def get_export_names(config=None):
  106. """Return a list of the currently supported export targets
  107. Exporters can be found in external packages by registering
  108. them as an nbconvert.exporter entrypoint.
  109. """
  110. exporters = sorted(e.name for e in entry_points(group="nbconvert.exporters"))
  111. if os.environ.get("NBCONVERT_DISABLE_CONFIG_EXPORTERS"):
  112. get_logger().info(
  113. "Config exporter loading disabled, no additional exporters will be automatically included."
  114. )
  115. return exporters
  116. if config is None:
  117. config = get_config()
  118. enabled_exporters = []
  119. for exporter_name in exporters:
  120. try:
  121. e = get_exporter(exporter_name)(config=config)
  122. if e.enabled:
  123. enabled_exporters.append(exporter_name)
  124. except (ExporterDisabledError, ValueError):
  125. pass
  126. return enabled_exporters