{"id":7553,"date":"2022-07-30T00:00:00","date_gmt":"2022-07-30T00:00:00","guid":{"rendered":"https:\/\/academy.deepsim.caawesome-features-of-jupyter-notebook-1-different-methods-to-create-python-files-949bf31c2c53\/"},"modified":"2022-11-25T03:58:41","modified_gmt":"2022-11-25T03:58:41","slug":"awesome-features-of-jupyter-notebook-1-different-methods-to-create-python-files-949bf31c2c53","status":"publish","type":"post","link":"https:\/\/academy.deepsim.ca\/zh\/awesome-features-of-jupyter-notebook-1-different-methods-to-create-python-files-949bf31c2c53\/","title":{"rendered":"Jupyter Notebook\u521b\u5efaPython\u6587\u4ef6\u7684\u65b9\u6cd5"},"content":{"rendered":"<p id=\"5d8b\" style=\"text-align: center;\"><em>-Jupyter Notebook is one excellent IDE for Python Developers<\/em><\/p>\n<p>&nbsp;<\/p>\n<p id=\"32ac\">\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u00a0<code>.py<\/code> \u6587\u4ef6\u5f88\u5bb9\u6613\u5728\u5176\u4ed6IDE\uff0c\u5982PyCharm\uff0cVS\u7801\uff0cSpyder\uff0c\u53ea\u6709\u540d\u79f0\u7684\u5c11\u6570\u3002 \u8bb8\u591a\u4eba\u8ba4\u4e3aJupyter\u6ce8\uff0c\u53ef\u4ee5\u521b\u9020\u00a0<code>.ipynb<\/code> \u6269\u5c55\u6587\u4ef6\uff0c\u8fd9\u662f\u77ed\u671f\u7684 <strong>IPython Jupyter Notebook<\/strong>. \u5b9e\u9645\u4e0a\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u521b\u5efa\u7075\u6d3b\u7684\u87d2\u86c7(<code>.py<\/code>)\u7684\u6269\u5c55\u7684\u6587\u4ef6\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u663e\u793a\u7684\u56db\u4e2a\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u521b\u5efaPython(<code>.py<\/code>)\u6269\u5c55\u6587\u4ef6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u53ef\u76f4\u63a5\u8fd0\u884c\u5728\u5176\u4ed6Python Ide\u3002<\/p>\n<h3 id=\"29f0\">1. \u4f7f\u7528\u5efa\u7acb\u5728\u6587\u672c\u7f16\u8f91\u5668<\/h3>\n<p id=\"311d\" class=\"translation-block\">\u5728Jupyter notebook\u63a7\u5236\u9762\u677f\u4e2d\uff0c\u53bb\"New\"\u83dc\u5355\uff0c \u5e76\u9009\u62e9\u201cText File\u201d \u6253\u5f00\u6587\u672c\u7f16\u8f91\u5668\u3002<\/p>\n<figure><img decoding=\"async\" class=\"alignleft\" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/08\/1z4nu698ukKuIPrtd3cXScw.png\" alt=\"\" \/><figcaption>\n<p>\n\u56fe 1. \u6253\u5f00Jupyter notebook\u5185\u7f6e\u6587\u672c\u7f16\u8f91\u5668<\/figcaption><\/figure>\n<p id=\"8e41\" class=\"translation-block\">\u9996\u5148\u91cd\u547d\u540d\u6587\u4ef6\uff0c\u4ee5 .py\u6269\u5c55 \u7684\u4efb\u4f55\u540d\u5b57\uff0c\u4f8b\u5982\uff0c\u91cd\u547d\u540d\u7684\u6587\u4ef6\u4e3a hi.py. \u5728\u8fd9\u91cc\u8981\u5f3a\u8c03\u7684\u662f\uff0c\u53ea\u6709\u5728\u60a8\u56e0\u4e3a\u5728\u547d\u540d .py\u6587\u4ef6\u540e\uff0c\u60a8\u5199\u4ee3\u7801\u662f\u5c06\u81ea\u52a8\u7f29\u8fdb\uff0c\u5e76\u6539\u53d8\u989c\u8272\uff0cJupyter notebook\u5185\u7f6e\u7684\u6587\u672c\u7f16\u8bd1\u5668\u80fd\u591f\u8fa8\u8bc6\u6240\u5199\u7684\u4ee3\u7801\u662fPython\u4ee3\u7801\u3002<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/875\/1*Vrwi8nctrozh5OWFPwc09Q.png\" \/><\/p>\n<p style=\"text-align: left;\"><span style=\"color: initial;\">\u56fe2. \u9996\u5148\u66f4\u6539\u6587\u4ef6\u7684\u540d\u79f0<\/span><\/p>\n<\/figure>\n<p id=\"f8e0\">\u7136\u540e\uff0c\u4f8b\u5982\u5728\u6587\u672c\u7f16\u8bd1\u5668\u4e2d\u952e\u5165\u4ee5\u4e0b\u7b80\u5355\u7684Python\u7684\u7a0b\u5e8f\uff1a<\/p>\n<pre id=\"6af4\"><code><strong>def<\/strong> hi(name):\n    print(f\"Hi,{name}!\")<\/code><\/pre>\n<p id=\"5fa2\">\u7136\u540e\u8ba9\u6211\u4eec\u53bb <strong>\u6587\u4ef6<\/strong> menu in the text editor, and select <strong>\u4fdd\u5b58<\/strong> and the file will be saved into your working directory.<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/proxy\/1*084aM5-GNE5xaMnxJbGXOg.png\" \/><br \/><figcaption>\n\u56fe 3. \u4fdd\u5b58hi.py\u6587\u4ef6<\/figcaption><\/figure>\n<h3 id=\"f4cd\">2. \u4e0b\u8f7d\u4e3a\u00a0<code>.py<\/code><\/h3>\n<p id=\"284d\">\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u7b14\u8bb0\u672c\u540d\u4e3a <code>myname<\/code>\u548c\u7c7b\u578b\u4ee5\u4e0b\u811a\u672c\uff1a<\/p>\n<pre id=\"cd3b\"><code>name <strong>=<\/strong> \"Tom\"\nprint(f\"My name is {name}\")<\/code><\/pre>\n<p id=\"ca67\" class=\"translation-block\">\u53bb\u201cFile\u201d\uff0c\u9009\u62e9\u201cDownload as\u201d\u3002\u800c\u540e\u9009\u62e9Python (.py)\u3002<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/875\/1*AQ5bWlw5BE8SQdom9VaTTA.png\" \/><\/figure>\n<figure><span style=\"color: initial;\">\u56fe 4. \u4e0b\u8f7dJupyter notebook as .py\u6587\u4ef6<\/span><\/figure>\n<p id=\"3204\">\u6587\u4ef6\u4e0b\u8f7d\u5230\u60a8 <strong>default download folder<\/strong> \u800c\u4e0d\u662f\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<h3 id=\"5c3f\">3. \u4f7f\u7528\u9b54\u672f\u51fd\u6570\u521b\u5efa\u00a0<code>.py<\/code> \u6587\u4ef6<\/h3>\n<p id=\"fc6b\"><strong>(1) <\/strong><code><strong>%%writefile<\/strong><\/code><strong> \u9b54\u672f\u51fd\u6570<\/strong><\/p>\n<ul>\n<li id=\"b5c7\">\u5f00\u59cbPython\u7f16\u7801\u524d\uff0c <code>\u9996\u5148\uff0c\u952e\u5165%%write filename.py\uff0c<\/code>.<\/li>\n<li id=\"0757\">\u5982\u521b\u5efa\u4e00\u4e2a <code>hello.py<\/code> \u6587\u4ef6\uff0c\u5728Jupyter notebook \u201cCell\u201d\u5355\u5143\u4e2d\u952e\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ul>\n<pre id=\"26bd\"><code><strong>%%<\/strong>writefile hello<strong>.<\/strong>py\r\n<strong>def<\/strong> hello(name):\n  print(f\"Hello, {name}!\")<\/code><\/pre>\n<p id=\"a462\">\u7136\u540e\u70b9\u51fb&#034;\u8fd0\u884c&#034;\uff0c\u5176\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/875\/1*V2y_WUW_lInym_I6zXUnWQ.png\" \/><\/figure>\n<figure><span style=\"color: initial;\">Fig. 5. Screenshot of the %%writefile magic result<\/span><\/figure>\n<p id=\"87c8\">\u5b83\u8f93\u51fa\u7ed3\u679c\"writing hello.py\"\uff0c\u8868\u660e\u8be5\u6587\u4ef6\u5df2\u88ab\u4fdd\u5b58\u5230\u60a8\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\u3002<\/p>\n<p id=\"0928\">\u8fd9\u662f\u6700\u4fbf\u6377\u3001\u6700\u7075\u6d3b\u7684\u521b\u5efa\u00a0<code>.py<\/code> \u6587\u4ef6\u7684\u65b9\u6cd5\u3002\u8fd9\u6837 \u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u4f55notebook\u4e2dPython\u4ee3\u7801\u8f6c\u6362\u4e3aPython\u6587\u4ef6\u3002<code>\u9996\u5148\uff0c\u952e\u5165%%write filename.py\uff0c<\/code> \u63a5\u7740\u952e\u5165\u7684\u8981\u5199Python\u4ee3\u7801\u3002<\/p>\n<p id=\"3de3\"><strong>(2) <\/strong><code><strong>%%file<\/strong><\/code><strong> \u51fd\u6570<\/strong><\/p>\n<p id=\"138b\"><code>%%file<\/code> \u7c7b\u4f3c\u4e8e <code>%%writefile<\/code>\u3002\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u5728Jupyter notebook\u4e2d\u521b\u5efa\u4e00\u4e2a\"greeting.py\"\u6587\u4ef6\u3002\u952e\u5165\u548c\u8fd0\u884c\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre id=\"a939\"><code><strong>%%<\/strong>file greeting<strong>.<\/strong>py\r\n<strong>def<\/strong> hello(fname,lname):\n    print(f\"Hello, {fname} {lname}!\")<\/code><\/pre>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/875\/1*dIrPqOwHoQQ1p9xibeOofg.png\" \/><\/figure>\n<figure><span style=\"color: initial;\">Fig. 6. Screenshot of the %%file magic result<\/span><\/figure>\n<h3 id=\"64da\">\u603b\u7ed3\uff1a<\/h3>\n<p id=\"82c2\">This article introduces you 4 convenient methods to create Python (<code>.py<\/code>) extension files directly in Jupyter notebook app, including using built-in text editor, download Jupyter notebook as <code>.py<\/code> \u6587\u4ef6\u548c\u4f7f\u7528 <code>%%writefile<\/code>\u548c <code>%%file<\/code>\u9b54\u672f\u51fd\u6570\u3002<\/p>\n<p id=\"7ee0\">\u5982\u679c\u4f60\u6709\u5174\u8da3\u7cfb\u7edf\u5b66\u4e60Jupyter Notebook\u8bfe\u7a0b\uff0c\u60a8\u53ef\u4ee5\u53c2\u52a0\u6211\u7684\u8bfe\u7a0b\uff1a\u00a0 <a href=\"https:\/\/academy.deepsim.cacourses\/practical-jupyter-notebook-from-beginner-to-expert\/\">\u5b9e\u7528Jupyter Notebook \u4ece\u5165\u95e8\u5230\u4e13\u5bb6\uff0c\u6b64\u8bfe\u6388\u8bfe\u8bed\u8a00\u6587\u82f1\u8bed\u3002<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>-Jupyter Notebook is one excellent IDE for Python Developers &nbsp; We can create\u00a0.py files easily in other IDE, such as<\/p>","protected":false},"author":2,"featured_media":7622,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66,64,65],"tags":[63,62],"class_list":["post-7553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jupyter-notebook","category-programming","category-python","tag-jupyter-notebook","tag-python"],"_links":{"self":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/comments?post=7553"}],"version-history":[{"count":0,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7553\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media\/7622"}],"wp:attachment":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media?parent=7553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/categories?post=7553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/tags?post=7553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}