{"id":4758,"date":"2022-07-13T11:18:31","date_gmt":"2022-07-13T03:18:31","guid":{"rendered":"http:\/\/139.9.1.231\/?p=4758"},"modified":"2022-07-13T11:18:32","modified_gmt":"2022-07-13T03:18:32","slug":"ipythonjupyter","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/07\/13\/ipythonjupyter\/","title":{"rendered":"IPython\uff08jupyter\uff09\u4e2d\u7684\u5e38\u7528\u5de5\u5177"},"content":{"rendered":"\n<p><code>ipython<\/code>\u662f\u4e00\u4e2a<code>python<\/code>\u7684\u4ea4\u4e92\u5f0f<code>shell<\/code>\uff0c\u6bd4\u9ed8\u8ba4\u7684<code>python shell<\/code>\u597d\u7528\u5f97\u591a\uff0c\u652f\u6301\u53d8\u91cf\u81ea\u52a8\u8865\u5168\uff0c\u81ea\u52a8\u7f29\u8fdb\uff0c\u652f\u6301<code>bash shell<\/code>\u547d\u4ee4\uff0c\u5185\u7f6e\u4e86\u8bb8\u591a\u5f88\u6709\u7528\u7684\u529f\u80fd\u548c\u51fd\u6570\u3002\u5b66\u4e60<code>ipython<\/code>\u5c06\u4f1a\u8ba9\u6211\u4eec\u4ee5\u4e00\u79cd\u66f4\u9ad8\u7684\u6548\u7387\u6765\u4f7f\u7528<code>python<\/code>\u3002\u540c\u65f6\u5b83\u4e5f\u662f\u5229\u7528Python\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u4ea4\u4e92\u53ef\u89c6\u5316\u7684\u4e00\u4e2a\u6700\u4f73\u7684\u5e73\u53f0\u3002<\/p>\n\n\n\n<p>IPython\u63d0\u4f9b\u4e86\u4e24\u4e2a\u4e3b\u8981\u7684\u7ec4\u4ef6\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>1.\u4e00\u4e2a\u5f3a\u5927\u7684python\u4ea4\u4e92\u5f0fshell<br>2.\u4f9bJupyter notebooks\u4f7f\u7528\u7684\u4e00\u4e2aJupyter\u5185\u6838\uff08IPython notebook\uff09<\/p><\/blockquote>\n\n\n\n<p>IPython\u7684\u4e3b\u8981\u529f\u80fd\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>1.\u8fd0\u884cipython\u63a7\u5236\u53f0<br>2.\u4f7f\u7528ipython\u4f5c\u4e3a\u7cfb\u7edfshell<br>3.\u4f7f\u7528\u5386\u53f2\u8f93\u5165(history)<br>4.Tab\u8865\u5168<br>5.\u4f7f\u7528<code>%run<\/code>\u547d\u4ee4\u8fd0\u884c\u811a\u672c<br>6.\u4f7f\u7528<code>%timeit<\/code>\u547d\u4ee4\u5feb\u901f\u6d4b\u91cf\u65f6\u95f4<br>7.\u4f7f\u7528<code>%pdb<\/code>\u547d\u4ee4\u5feb\u901fdebug<br>8.\u4f7f\u7528<code>pylab<\/code>\u8fdb\u884c\u4ea4\u4e92\u8ba1\u7b97<br>9.\u4f7f\u7528IPython Notebook<\/p><p><\/p><\/blockquote>\n\n\n\n<h2 id=\"31-tab\u952e\u81ea\u52a8\u8865\u5168\"><strong>Tab\u952e\u81ea\u52a8\u8865\u5168<\/strong><\/h2>\n\n\n\n<p>\u5728shell\u4e2d\u8f93\u5165\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ea\u8981\u6309\u4e0bTab\u952e\uff0c\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u4efb\u4f55\u4e0e\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u76f8\u5339\u914d\u7684\u53d8\u91cf(\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u7b49)\u5c31\u4f1a\u88ab\u627e\u51fa\u6765<\/p>\n\n\n\n<h2 class=\"has-light-pink-background-color has-background\" id=\"32-\u5185\u7701\"><strong>&nbsp;\u5185\u7701<\/strong><\/h2>\n\n\n\n<p>\u5728\u53d8\u91cf\u7684\u524d\u9762\u6216\u8005\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u95ee\u53f7<code>?<\/code>\uff0c\u5c31\u53ef\u4ee5\u5c06\u6709\u5173\u8be5\u5bf9\u8c61\u7684\u4e00\u4e9b\u901a\u7528\u4fe1\u606f\u663e\u793a\u51fa\u6765\uff0c\u8fd9\u5c31\u53eb\u505a\u5bf9\u8c61\u7684\u5185\u7701<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"972\" height=\"612\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-11.png\" alt=\"\" class=\"wp-image-4761\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-11.png 972w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-11-300x189.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-11-768x484.png 768w\" sizes=\"(max-width: 972px) 100vw, 972px\" \/><\/figure>\n\n\n\n<p>\u5982\u679c\u5bf9\u8c61\u662f\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u5b9e\u4f8b\u65b9\u6cd5\uff0c\u5219\u5b83\u7684docstring\u4e5f\u4f1a\u88ab\u663e\u793a\u51fa\u6765<\/p>\n\n\n\n<h2 id=\"33-\u4f7f\u7528\u5386\u53f2\u547d\u4ee4history\"><strong>\u4f7f\u7528\u5386\u53f2\u547d\u4ee4history<\/strong><\/h2>\n\n\n\n<p>\u5728IPython shell\u4e2d\uff0c\u4f7f\u7528\u5386\u53f2\u547d\u4ee4\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u4e0a\u4e0b\u7ffb\u9875\u952e\u5373\u53ef\uff0c\u53e6\u5916\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>hist<\/code>\u547d\u4ee4(\u6216\u8005<code>history<\/code>\u547d\u4ee4)\u67e5\u770b\u6240\u6709\u7684\u5386\u53f2\u8f93\u5165\u3002\uff08\u6b63\u786e\u7684\u505a\u6cd5\u662f\u4f7f\u7528<code>%hist<\/code>\uff0c\u5728\u8fd9\u91cc\uff0c<code>%hist<\/code>\u4e5f\u662f\u4e00\u4e2a\u9b54\u6cd5\u547d\u4ee4\uff09<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" width=\"289\" height=\"255\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-12.png\" alt=\"\" class=\"wp-image-4763\"\/><\/figure><\/div>\n\n\n\n<h2 class=\"has-light-pink-background-color has-background\" id=\"34-\u4f7f\u7528run\u547d\u4ee4\u8fd0\u884c\u811a\u672c\"><strong>\u4f7f\u7528<code>%run<\/code>\u547d\u4ee4\u8fd0\u884c\u811a\u672c<\/strong><\/h2>\n\n\n\n<p>\u5728ipython\u4f1a\u8bdd\u73af\u5883\u4e2d\uff0c\u6240\u6709\u6587\u4ef6\u90fd\u53ef\u4ee5\u901a\u8fc7<code>%run<\/code>\u547d\u4ee4\u5f53\u505aPython\u7a0b\u5e8f\u6765\u8fd0\u884c\uff0c\u8f93\u5165<code>%run \u8def\u5f84+python\u6587\u4ef6\u540d\u79f0\u5373\u53ef<\/code><\/p>\n\n\n\n<h2 id=\"35-\u4f7f\u7528timeit\u547d\u4ee4\u5feb\u901f\u6d4b\u91cf\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\"><strong>\u4f7f\u7528<code>%timeit<\/code>\u547d\u4ee4\u5feb\u901f\u6d4b\u91cf\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4<\/strong><\/h2>\n\n\n\n<p>\u5728\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>%timeit<\/code>\u9b54\u6cd5\u547d\u4ee4\u5feb\u901f\u6d4b\u91cf\u4ee3\u7801\u8fd0\u884c\u65f6\u95f4\u3002\u76f8\u540c\u7684\u547d\u4ee4\u4f1a\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u591a\u6b21\u6267\u884c\uff0c\u591a\u6b21\u8fd0\u884c\u65f6\u957f\u7684\u5e73\u5747\u503c\u4f5c\u4e3a\u8be5\u547d\u4ee4\u7684\u6700\u7ec8\u8bc4\u4f30\u65f6\u957f\u3002<code>-n<\/code>&nbsp;\u9009\u9879\u53ef\u4ee5\u63a7\u5236\u547d\u4ee4\u5728\u5355\u8bcd\u5faa\u73af\u4e2d\u6267\u884c\u7684\u6b21\u6570\uff0c<code>-r<\/code>\u9009\u9879\u63a7\u5236\u6267\u884c\u5faa\u73af\u7684\u6b21\u6570\u3002<\/p>\n\n\n\n<h2 id=\"36-\u4f7f\u7528debug\u547d\u4ee4\u8fdb\u884c\u5feb\u901fdebug\"><strong>\u4f7f\u7528<code>%debug<\/code>\u547d\u4ee4\u8fdb\u884c\u5feb\u901fdebug<\/strong><\/h2>\n\n\n\n<p>ipython\u5e26\u6709\u4e00\u4e2a\u5f3a\u5927\u7684\u8c03\u8bd5\u5668\u3002\u65e0\u8bba\u4f55\u65f6\u63a7\u5236\u53f0\u629b\u51fa\u4e86\u4e00\u4e2a\u5f02\u5e38\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u4f7f\u7528<code>%debug<\/code>\u9b54\u6cd5\u547d\u4ee4\u5728\u5f02\u5e38\u70b9\u542f\u52a8\u8c03\u8bd5\u5668\u3002\u63a5\u7740\u4f60\u5c31\u80fd\u8c03\u8bd5\u6a21\u5f0f\u4e0b\u8bbf\u95ee\u6240\u6709\u7684\u672c\u5730\u53d8\u91cf\u548c\u6574\u4e2a\u6808\u56de\u6eaf\u3002\u4f7f\u7528<code>u<\/code>\u548c<code>d<\/code>\u5411\u4e0a\u548c\u5411\u4e0b\u8bbf\u95ee\u6808\uff0c\u4f7f\u7528<code>q<\/code>\u9000\u51fa\u8c03\u8bd5\u5668\u3002\u5728\u8c03\u8bd5\u5668\u4e2d\u8f93\u5165<code>?<\/code>\u53ef\u4ee5\u67e5\u770b\u6240\u6709\u7684\u53ef\u7528\u547d\u4ee4\u5217\u8868\u3002<\/p>\n\n\n\n<h2 id=\"38-\u5728ipython\u4e2d\u4f7f\u7528\u7cfb\u7edfshell\"><strong>&nbsp;\u5728IPython\u4e2d\u4f7f\u7528\u7cfb\u7edfshell<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5728IPython\u4e2d\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edfshell\uff0c\u5e76\u83b7\u53d6\u8bfb\u53d6\u7ed3\u679c\u4f5c\u4e3a\u4e00\u4e2aPython\u5b57\u7b26\u4e32\u5217\u8868\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u79cd\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u611f\u53f9\u53f7<code>!<\/code>\u4f5c\u4e3ashell\u547d\u4ee4\u7684\u524d\u7f00\u3002\u6bd4\u5982\u73b0\u5728\u5728\u6211\u7684windows\u7cfb\u7edf\u4e2d\uff0c\u76f4\u63a5\u5728IPython\u4e2dping\u767e\u5ea6<\/p>\n\n\n\n<h2 class=\"has-medium-pink-background-color has-background\">\u91cd<strong>\u70b9\uff1a<\/strong><code>display<\/code> \u6a21\u5757<\/h2>\n\n\n\n<p class=\"has-purple-color has-light-gray-background-color has-text-color has-background\">\u5b98\u65b9\u6559\u7a0b  <a href=\"https:\/\/ipython.readthedocs.io\/en\/stable\/api\/generated\/IPython.display.html\">https:\/\/ipython.readthedocs.io\/en\/stable\/api\/generated\/IPython.display.html<\/a><\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u4e4b\u524d\u60f3\u8981\u5728jupyter\u4e2d\u663e\u793a\u56fe\u50cf \u3001\u89c6\u9891 or voice\u3001html\uff0c\u53ef\u80fd\u4e0d\u77e5\u9053\u8be5\u600e\u4e48\u529e\uff0c\u6709\u4e86IP  display\u6a21\u5757\uff0c\u53ef\u4ee5\u89e3\u51b3\u8be5\u95ee\u9898\u3002<\/p>\n\n\n\n<p>1\u3001audio<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from IPython.display import Audio,display\r\nsound_file = '..\/taobao427.mp3'\r\ndisplay(Audio(sound_file))\r\n<\/code><\/pre>\n\n\n\n<p>2\u3001ipython.display.image<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\r\n\r\nfrom IPython.display import display, Image\r\n\r\npath = \"1.jpg\"\r\n\r\ndisplay( Image( filename = path) )\r\n<\/code><\/pre>\n\n\n\n<p>3\u3001\u64ad\u653e\u89c6\u9891<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from IPython.display import clear_output,  display, HTML\r\nfrom PIL import Image\r\nimport matplotlib.pyplot as plt\r\nimport time\r\nimport cv2\r\nimport os\r\n\r\ndef show_video(video_path:str,small:int=2):\r\n    if not os.path.exists(video_path):\r\n        print(\"\u89c6\u9891\u6587\u4ef6\u4e0d\u5b58\u5728\")\r\n    video = cv2.VideoCapture(video_path)\r\n    current_time = 0\r\n    while(True):\r\n        try:\r\n            clear_output(wait=True)\r\n            ret, frame = video.read()\r\n            if not ret:\r\n                break\r\n            lines, columns, _ = frame.shape\r\n            #########do img preprocess##########\r\n            \r\n            # \u753b\u51fa\u4e00\u4e2a\u6846\r\n            #     cv2.rectangle(img, (500, 300), (800, 400), (0, 0, 255), 5, 1, 0)\r\n             # \u4e0a\u4e0b\u7ffb\u8f6c\r\n             # img= cv2.flip(img, 0)\r\n            \r\n            ###################################\r\n            \r\n            if current_time == 0:\r\n                current_time = time.time()\r\n            else:\r\n                last_time = current_time\r\n                current_time = time.time()\r\n                fps = 1. \/ (current_time - last_time)\r\n                text = \"FPS: %d\" % int(fps)\r\n                cv2.putText(frame, text , (0,100), cv2.FONT_HERSHEY_TRIPLEX, 3.65, (255, 0, 0), 2)\r\n                \r\n          #     img = cv2.resize(img,(1080,1080))\r\n                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\r\n            frame = cv2.resize(frame, (int(columns \/ small), int(lines \/ small)))\r\n\r\n            img = Image.fromarray(frame)\r\n\r\n            display(img)\r\n            # \u63a7\u5236\u5e27\u7387\r\n            time.sleep(0.02)\r\n        except KeyboardInterrupt:\r\n            video.release()<\/code><\/pre>\n\n\n\n<p>4\u3001htlm\uff08\u89c6\u9891\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># ########## display\r\nfrom IPython.display import display, HTML\r\n\r\nhtml_str = '''\r\n&lt;video controls width=\\\"500\\\" height=\\\"500\\\" src=\\\"{}\\\">animation&lt;\/video>\r\n'''.format(\".\/dataset\/vid****8726.mp4\")\r\nprint(html_str)\r\ndisplay(HTML(html_str))<\/code><\/pre>\n\n\n\n<p>5\u3001\u63d2\u5165\u53c2\u8003\u7684\u7f51\u9875\u6216\u8005\u8bba\u6587 iframe<\/p>\n\n\n\n<p><code>from IPython.display import IFrame<\/code>\u00a0<code>IFrame(src='https:\/\/www.baidu.com\/',width=800,height=500)<\/code><\/p>\n\n\n\n<p><code>from IPython.display import HTML<\/code><code><br><\/code><code>HTML(\"\"\"<\/code><\/p>\n\n\n\n<p><code>Example Domain<\/code><\/p>\n\n\n\n<p><code>This domain is for use in illustrative examples in documents. You may use this<\/code>&nbsp;<code>domain in literature without prior coordination or asking for permission.<\/code><\/p>\n\n\n\n<p><code>More information...<\/code><\/p>\n\n\n\n<p><code>\"\"\")<\/code><\/p>\n\n\n\n<p>6\u3001\u63d2\u5165iframe\u6807\u7b7e<\/p>\n\n\n\n<p><code>from IPython.display import HTML<br>HTML('')<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"545\" height=\"230\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-13.png\" alt=\"\" class=\"wp-image-4779\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-13.png 545w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/07\/image-13-300x127.png 300w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>ipython\u662f\u4e00\u4e2apython\u7684\u4ea4\u4e92\u5f0fshell\uff0c\u6bd4\u9ed8\u8ba4\u7684python shell\u597d\u7528\u5f97\u591a\uff0c\u652f\u6301\u53d8\u91cf\u81ea\u52a8\u8865\u5168 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/07\/13\/ipythonjupyter\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">IPython\uff08jupyter\uff09\u4e2d\u7684\u5e38\u7528\u5de5\u5177<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/4758"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=4758"}],"version-history":[{"count":20,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/4758\/revisions"}],"predecessor-version":[{"id":4883,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/4758\/revisions\/4883"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=4758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=4758"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=4758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}