utils_34.py 911 B

123456789101112131415161718192021
  1. def _post_save_script(model, os_path, contents_manager, **kwargs):
  2. """convert notebooks to Python script after save with nbconvert
  3. replaces `jupyter notebook --script`
  4. """
  5. from nbconvert.exporters.script import ScriptExporter
  6. warnings.warn("`_post_save_script` is deprecated and will be removed in Notebook 5.0", DeprecationWarning)
  7. if model['type'] != 'notebook':
  8. return
  9. global _script_exporter
  10. if _script_exporter is None:
  11. _script_exporter = ScriptExporter(parent=contents_manager)
  12. log = contents_manager.log
  13. base, ext = os.path.splitext(os_path)
  14. script, resources = _script_exporter.from_filename(os_path)
  15. script_fname = base + resources.get('output_extension', '.txt')
  16. log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
  17. with io.open(script_fname, 'w', encoding='utf-8') as f:
  18. f.write(script)