{"id":269,"date":"2024-09-07T01:22:16","date_gmt":"2024-09-06T16:22:16","guid":{"rendered":"https:\/\/portal-sight.com\/wp1\/?p=269"},"modified":"2024-09-07T01:22:17","modified_gmt":"2024-09-06T16:22:17","slug":"jupyter-labnotebook%e3%81%a7ipynb%e3%82%92py%e3%81%a7%e5%87%ba%e5%8a%9b","status":"publish","type":"post","link":"https:\/\/portal-sight.com\/wp1\/?p=269","title":{"rendered":"jupyter lab(notebook)\u3067ipynb\u3092py\u3067\u51fa\u529b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">python\u306e\u958b\u767a\u74b0\u5883\u3067\u30d6\u30e9\u30a6\u30b6\u3060\u3051\u3067\u4f7f\u3048\u308b\u306e\u3067\u6700\u8fd1\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u3044\u308bJupyter notebook, jupyter lab \u3067\u6700\u7d42\u7684\u306bpy\u3092\u51fa\u529b\u3059\u308b\u306e\u306f\u30e1\u30cb\u30e5\u30fc\u306eexport\u3067\u3084\u308b\u3068\u3057\u3066\u3001ipynb\u304b\u3089py\u30d5\u30a1\u30a4\u30eb\u3092\u51fa\u529b\u3057\u3066\u305d\u306epy\u30d5\u30a1\u30a4\u30eb\u3067\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\u3092\u3059\u308b\u3068\u304d\u306b\u3001jupyter_lab_config.py(notebook\u306e\u5834\u5408\u306fjupyter_notebook_config.py)\u3092\u7de8\u96c6\u3059\u308b\u65b9\u6cd5\u304c\u7c21\u5358\u4fbf\u5229\u307f\u305f\u3044\u3060\u3051\u3069\u3001to_api_path\u306e\u3068\u3053\u308d\u3067\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u306e\u3067\u3001\u30cd\u30c3\u30c8\u306b\u51fa\u3066\u3044\u308b\u30b5\u30f3\u30d7\u30eb\u3092\u66f8\u304d\u63db\u3048\u305f\u30e1\u30e2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import io\nimport os\n#<s>from notebook.utils import to_api_path<\/s> #to_api_path\u306efrom\u3092\u66f8\u304d\u63db\u3048\u308b\nfrom jupyter_server.utils import to_api_path\n\n_script_exporter = None\n\ndef script_post_save(model, os_path, contents_manager, **kwargs):\n    \"\"\"convert notebooks to Python script after save with nbconvert\n\n    replaces `ipython notebook --script`\n    \"\"\"\n    from nbconvert.exporters.script import ScriptExporter\n\n    if model&#91;'type'] != 'notebook':\n        return\n    # https:\/\/qiita.com\/shiho_h\/items\/6f1dd3fb1b9136416ec6\n    if model&#91;'last_modified'] - model&#91;'created'] &lt; datetime.timedelta(seconds=1):\n        return # \u3010\u8ffd\u52a0\u3011create\u3068last_modified\u306e\u5dee\u304c\u5c0f\u3055\u3051\u308c\u3070, \u65b0\u898f\u4f5c\u6210\u3068\u5224\u65ad\u3057\u3066\u306a\u306b\u3082\u3057\u306a\u3044\n    global _script_exporter\n    if _script_exporter is None:\n        _script_exporter = ScriptExporter(parent=contents_manager)\n    log = contents_manager.log\n\n    base, ext = os.path.splitext(os_path)\n    py_fname = base + '.py'\n    script, resources = _script_exporter.from_filename(os_path)\n    script_fname = base + resources.get('output_extension', '.txt')\n#    log.info(\"Saving script \/%s\", to_api_path(script_fname, contents_manager.root_dir))\n    with io.open(script_fname, 'w', encoding='utf-8') as f:\n        f.write(script)\nc.FileContentsManager.post_save_hook = script_post_save\n\n\n\n## Python callable or importstring thereof\n#  See also: ContentsManager.pre_save_hook\n# c.FileContentsManager.pre_save_hook = None\ndef scrub_output_pre_save(model, **kwargs):\n    \"\"\"scrub output before saving notebooks\"\"\"\n    # only run on notebooks\n    if model&#91;'type'] != 'notebook':\n        return\n    # only run on nbformat v4\n    if model&#91;'content']&#91;'nbformat'] != 4:\n        return\n\n    for cell in model&#91;'content']&#91;'cells']:\n        if cell&#91;'cell_type'] != 'code':\n            continue\n        cell&#91;'outputs'] = &#91;]\n        cell&#91;'execution_count'] = None\n\nc.FileContentsManager.pre_save_hook = scrub_output_pre_save\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u306e\u958b\u767a\u74b0\u5883\u3067\u30d6\u30e9\u30a6\u30b6\u3060\u3051\u3067\u4f7f\u3048\u308b\u306e\u3067\u6700\u8fd1\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u3044\u308bJupyter notebook, jupyter lab \u3067\u6700\u7d42\u7684\u306bpy\u3092\u51fa\u529b\u3059\u308b\u306e\u306f\u30e1\u30cb\u30e5\u30fc\u306eexport\u3067\u3084\u308b\u3068\u3057\u3066\u3001ipynb\u304b\u3089py\u30d5\u30a1\u30a4\u30eb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-1"],"_links":{"self":[{"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=\/wp\/v2\/posts\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269"}],"version-history":[{"count":2,"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":271,"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions\/271"}],"wp:attachment":[{"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/portal-sight.com\/wp1\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}