{"id":1566,"date":"2024-11-24T20:32:12","date_gmt":"2024-11-24T12:32:12","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=1566"},"modified":"2024-11-24T20:32:13","modified_gmt":"2024-11-24T12:32:13","slug":"%e5%a6%82%e4%bd%95%e7%bb%84%e7%bb%87%e4%bd%a0%e7%9a%84-jupyter-notebook","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=1566","title":{"rendered":"\u5982\u4f55\u7ec4\u7ec7\u4f60\u7684 jupyter notebook"},"content":{"rendered":"<p>\u5728\u4e00\u4e2a Jupyter Notebook \u4e2d\u5f15\u7528\u53e6\u4e00\u4e2a Notebook \u7684 Python \u51fd\u6570\u6216\u7c7b\u6709\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u9879\u76ee\u7ed3\u6784\uff1a<\/p>\n<h2>\u65b9\u6cd5 1: \u4f7f\u7528 import_ipynb \u6a21\u5757<\/h2>\n<p>\u8fd9\u662f\u4e13\u95e8\u4e3a\u5bfc\u5165 Jupyter Notebook \u4e2d\u7684\u4ee3\u7801\u800c\u8bbe\u8ba1\u7684\u65b9\u6cd5\u3002<\/p>\n<pre><code>1.  \u5b89\u88c5 import_ipynb<\/code><\/pre>\n<pre><code>pip install import-ipynb<\/code><\/pre>\n<pre><code>2.  \u5bfc\u5165\u53e6\u4e00\u4e2a Notebook<\/code><\/pre>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a helper_functions.ipynb\uff0c\u4f60\u60f3\u5728\u5f53\u524d Notebook \u4e2d\u4f7f\u7528\u5b83\u7684\u5185\u5bb9\u3002<\/p>\n<pre><code class=\"language-python\">import import_ipynb\nimport helper_functions\n\n# \u4f7f\u7528 helper_functions \u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u6216\u7c7b\nresult = helper_functions.some_function()\nmy_class = helper_functions.SomeClass()<\/code><\/pre>\n<h2>\u65b9\u6cd5 2: \u5c06 Notebook \u8f6c\u6362\u4e3a Python \u6587\u4ef6<\/h2>\n<p>\u5c06\u53e6\u4e00\u4e2a Notebook \u8f6c\u6362\u4e3a .py \u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5176\u5bfc\u5165\u3002<\/p>\n<pre><code>1.  \u5c06 Notebook \u8f6c\u6362\u4e3a Python \u6587\u4ef6<\/code><\/pre>\n<p>\u5728 Jupyter Notebook \u754c\u9762\u4e2d\uff1a<br \/>\n\u2022   \u6253\u5f00\u8981\u5bfc\u5165\u7684 Notebook\u3002<br \/>\n\u2022   \u70b9\u51fb\u83dc\u5355\u680f\u4e2d\u7684 File -&gt; Download as -&gt; Python (.py)\u3002<br \/>\n\u2022   \u4f8b\u5982\uff0c\u4fdd\u5b58\u4e3a helper_functions.py\u3002<\/p>\n<pre><code>2.  \u5728\u5f53\u524d Notebook \u4e2d\u5bfc\u5165<\/code><\/pre>\n<pre><code class=\"language-python\">from helper_functions import some_function, SomeClass\n\nresult = some_function()\nmy_class = SomeClass()<\/code><\/pre>\n<p>\u5982\u679c .py \u6587\u4ef6\u548c\u5f53\u524d Notebook \u4e0d\u5728\u540c\u4e00\u76ee\u5f55\uff0c\u53ef\u4ee5\u6dfb\u52a0\u8def\u5f84\uff1a<\/p>\n<pre><code class=\"language-python\">import sys\nsys.path.append(&#039;\/path\/to\/directory&#039;)\nfrom helper_functions import some_function<\/code><\/pre>\n<h2>\u65b9\u6cd5 3: \u4f7f\u7528 nbconvert \u81ea\u52a8\u751f\u6210 Python \u6587\u4ef6<\/h2>\n<p>\u4f7f\u7528 nbconvert \u5de5\u5177\u81ea\u52a8\u4ece Notebook \u751f\u6210 .py \u6587\u4ef6\uff0c\u5e76\u52a8\u6001\u52a0\u8f7d\u3002<\/p>\n<pre><code>1.  \u5b89\u88c5 nbconvert<\/code><\/pre>\n<pre><code>pip install nbconvert\n<\/code><\/pre>\n<pre><code>2.  Notebook \u52a8\u6001\u8f6c\u6362\u5e76\u5bfc\u5165<\/code><\/pre>\n<pre><code>!jupyter nbconvert --to script helper_functions.ipynb\n\nfrom helper_functions import some_function\n\nresult = some_function()<\/code><\/pre>\n<h2>\u65b9\u6cd5 4: \u4f7f\u7528 exec \u52a8\u6001\u52a0\u8f7d Notebook<\/h2>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4ec5\u4e34\u65f6\u5bfc\u5165\u7684\u573a\u666f\u3002<\/p>\n<pre><code>1.  \u52a0\u8f7d Notebook \u7684\u4ee3\u7801<\/code><\/pre>\n<pre><code>with open(&#039;helper_functions.ipynb&#039;, &#039;r&#039;) as f:\n    notebook_content = f.read()\n\nimport json\nnotebook = json.loads(notebook_content)\n\n# \u63d0\u53d6\u4ee3\u7801\u5355\u5143\u683c\u5e76\u6267\u884c\nfor cell in notebook[&#039;cells&#039;]:\n    if cell[&#039;cell_type&#039;] == &#039;code&#039;:\n        exec(&#039;&#039;.join(cell[&#039;source&#039;]))\n\n    2.  \u4f7f\u7528\u5bfc\u5165\u7684\u51fd\u6570\u6216\u7c7b\n\nresult = some_function()\nmy_class = SomeClass()<\/code><\/pre>\n<h2>\u65b9\u6cd5 5: \u4f7f\u7528 jupytext \u5b9e\u73b0\u540c\u6b65<\/h2>\n<p>\u5c06 Notebook \u8f6c\u6362\u4e3a .py \u6587\u4ef6\u5e76\u53cc\u5411\u540c\u6b65\u3002<\/p>\n<pre><code>1.  \u5b89\u88c5 jupytext<\/code><\/pre>\n<pre><code>pip install jupytext<\/code><\/pre>\n<pre><code>2.  \u540c\u6b65 Notebook \u548c Python \u6587\u4ef6<\/code><\/pre>\n<p>\u5728\u9700\u8981\u540c\u6b65\u7684 Notebook \u4e2d\u542f\u7528 Jupytext\uff1a<br \/>\n\u2022   \u70b9\u51fb\u83dc\u5355\u680f File -&gt; Jupytext -&gt; Pair Notebook with light script\u3002<br \/>\n\u2022   \u4fdd\u5b58\u65f6\u4f1a\u81ea\u52a8\u751f\u6210 .py \u6587\u4ef6\u3002<\/p>\n<ol start=\"3\">\n<li>\u5bfc\u5165\u751f\u6210\u7684 .py \u6587\u4ef6<\/li>\n<\/ol>\n<pre><code class=\"language-python\">from helper_functions import some_function, SomeClass\n\nresult = some_function()\nmy_class = SomeClass()<\/code><\/pre>\n<h2>\u6700\u4f73\u5b9e\u8df5<\/h2>\n<ul>\n<li>\u4ee3\u7801\u91cd\u7528\u6027\uff1a\u5982\u679c\u4e00\u4e2a Notebook \u5305\u542b\u8f83\u591a\u53ef\u91cd\u7528\u4ee3\u7801\uff0c\u5efa\u8bae\u63d0\u53d6\u6210\u72ec\u7acb\u7684 Python \u6a21\u5757\u3002<\/li>\n<li>\u8def\u5f84\u7ba1\u7406\uff1a\u5c06\u6240\u6709 Notebooks \u548c\u6a21\u5757\u7ec4\u7ec7\u5728\u7edf\u4e00\u7684\u9879\u76ee\u7ed3\u6784\u4e0b\uff0c\u4f8b\u5982\uff1a\n<pre><code>\/project\n\u251c\u2500\u2500 notebooks\/\n\u2502   \u251c\u2500\u2500 main_notebook.ipynb\n\u2502   \u251c\u2500\u2500 helper_notebook.ipynb\n\u251c\u2500\u2500 utils\/\n\u2502   \u251c\u2500\u2500 helper_functions.py\n\u2514\u2500\u2500 requirements.txt<\/code><\/pre>\n<p>\u5728\u8fd9\u79cd\u7ed3\u6784\u4e0b\uff0c\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u4f9d\u8d56\uff0c\u5e76\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u673a\u5236\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff1a<\/p>\n<ul>\n<li>\u4e34\u65f6\u65b9\u6848\uff08\u5982 import_ipynb\uff09\uff1a\u65b9\u4fbf\u5feb\u901f\u5b9e\u9a8c\u3002<\/li>\n<li>\u6b63\u5f0f\u9879\u76ee\uff08\u5982\u751f\u6210 .py \u6587\u4ef6\u6216\u7528 jupytext\uff09\uff1a\u66f4\u9002\u5408\u957f\u671f\u7ef4\u62a4\u548c\u534f\u4f5c\u3002<br \/>\n\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684 Notebook \u66f4\u52a0\u9ad8\u6548\u3001\u6613\u7528\u548c\u6a21\u5757\u5316\uff01<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e00\u4e2a Jupyter Notebook \u4e2d\u5f15\u7528\u53e6\u4e00\u4e2a Notebook \u7684 Python \u51fd\u6570\u6216\u7c7b\u6709\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u9879\u76ee\u7ed3\u6784\uff1a \u65b9\u6cd5 1: \u4f7f\u7528 import_ipynb \u6a21\u5757 \u8fd9\u662f\u4e13\u95e8\u4e3a\u5bfc\u5165 Jupyter Notebook \u4e2d\u7684\u4ee3\u7801\u800c\u8bbe\u8ba1\u7684\u65b9\u6cd5\u3002 1. \u5b89\u88c5 import_ipynb pip install import-ipynb 2. \u5bfc\u5165\u53e6\u4e00\u4e2a Notebook \u5047\u8bbe\u6709\u4e00\u4e2a helper_functions.ipynb\uff0c\u4f60\u60f3\u5728\u5f53\u524d Notebook \u4e2d\u4f7f\u7528\u5b83\u7684\u5185\u5bb9\u3002 import import_ipynb import helper_functions # \u4f7f\u7528 helper_functions \u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u6216\u7c7b result = helper_functions.some_function() my_class = helper_functions.SomeClass() \u65b9\u6cd5 2: \u5c06 Notebook \u8f6c\u6362\u4e3a Python \u6587\u4ef6 \u5c06\u53e6\u4e00\u4e2a Notebook \u8f6c\u6362\u4e3a .py [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=1566\" title=\"Permanent Link to: \u5982\u4f55\u7ec4\u7ec7\u4f60\u7684 jupyter notebook\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1566","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1566"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1566"}],"version-history":[{"count":1,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1566\/revisions"}],"predecessor-version":[{"id":1567,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1566\/revisions\/1567"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}