{"id":1630,"date":"2018-03-17T22:01:20","date_gmt":"2018-03-17T13:01:20","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=1630"},"modified":"2018-11-04T22:35:53","modified_gmt":"2018-11-04T13:35:53","slug":"flask%e5%85%a5%e9%96%80","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/1630","title":{"rendered":"Flask\u5165\u9580"},"content":{"rendered":"<h2>Flask\u3068\u306f<\/h2>\n<p>Python\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u3044\u3048\u3070Django\u304c\u6709\u540d\u3067\u3059\u304c\u3001\u8efd\u91cf\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u306f\u6700\u8fd1Flask\u306e\u4eba\u6c17\u304c\u9ad8\u307e\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u975e\u5e38\u306b\u8584\u304f\u30de\u30a4\u30af\u30ed\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u8a00\u308f\u308c\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u305d\u306e\u5206\u5b66\u7fd2\u30b3\u30b9\u30c8\u306f\u975e\u5e38\u306b\u4f4e\u3044\u305f\u3081\u3001\u6577\u5c45\u306e\u4f4e\u3044\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u3044\u3048\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u8efd\u91cf\u3068\u306f\u3044\u3048\u3001URL\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u7b49\u3068\u3044\u3063\u305f\u57fa\u672c\u7684\u306a\u6a5f\u80fd\u306f\u4e88\u3081\u4f7f\u7528\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u305f\u3081\u3001\u7c21\u5358\u306aREST API\u3092\u3055\u304f\u3063\u3068\u4f5c\u308a\u305f\u3044\u5834\u5408\u306b\u306f\u975e\u5e38\u306b\u9069\u3057\u3066\u3044\u308b\u3068\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u8fd1\u5e74\u30d7\u30e9\u30b0\u30a4\u30f3\u304c\u975e\u5e38\u306b\u5145\u5b9f\u3057\u3066\u304d\u3066\u304a\u308a\u3001\u6a5f\u80fd\u3092\u62e1\u5f35\u3059\u308b\u3068\u69d8\u3005\u306a\u6a5f\u80fd\u3092\u7c21\u5358\u306b\u69cb\u7bc9\u3057\u305f\u308a\u3067\u304d\u308b\u305f\u3081\u3001\u3042\u308b\u7a0b\u5ea6\u306e\u898f\u6a21\u306e\u30b7\u30b9\u30c6\u30e0\u306e\u69cb\u7bc9\u306b\u3082\u8010\u3048\u3089\u308c\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u307e\u305f\u3001\u3082\u3046\uff11\u70b9\u5229\u7528\u5c40\u9762\u3068\u3057\u3066\u304a\u3059\u3059\u3081\u3057\u305f\u3044\u306e\u304c\u3001\u793e\u5185\u3060\u3051\u3067\u4f7f\u3046\u3088\u3046\u306a\u7c21\u5358\u306a\u5206\u6790\u30c4\u30fc\u30eb\u306e\u69cb\u7bc9\u3067\u3059\u3002<\/p>\n<p>\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3068\u5206\u6790\u305f\u7d50\u679c\u3092\u6574\u7406\u3057\u305f\u308a\u7ba1\u7406\u3059\u308b\u306e\u306f\u3061\u3087\u3063\u3068\u9762\u5012\u3067\u3059\u304c\u3001Web\u5316\u3057\u3066\u7d50\u679c\u3092DB\u3067\u7ba1\u7406\u3057\u3066\u304a\u304f\u3068\u3044\u3064\u3067\u3082\u898b\u3089\u308c\u308b\u3057\u3001\u4eba\u306b\u898b\u305b\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u6b21\u56de\u4ee5\u964d\u3001\u7c21\u5358\u306b\u4f5c\u308c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb\u306e\u69cb\u7bc9\u4f8b\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2>\u7c21\u5358\u306b\u4f7f\u3063\u3066\u307f\u308b<\/h2>\n<p>\u3053\u3053\u3067\u306fFlask\u3092\u89e6\u3063\u305f\u3053\u3068\u304c\u306a\u3044\u65b9\u5411\u3051\u306b\u7c21\u5358\u306b\u4f7f\u3044\u65b9\u3092\u8aac\u660e\u3057\u307e\u3059\u3002\u8a73\u3057\u304f\u77e5\u308a\u305f\u3044\u65b9\u306f\u4ee5\u4e0b\u306e\u516c\u5f0f\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u304c\u304a\u3059\u3059\u3081\u3067\u3059\u3002<\/p>\n<p><a href=\"http:\/\/flask.pocoo.org\/docs\/0.12\/tutorial\/\">http:\/\/flask.pocoo.org\/docs\/0.12\/tutorial\/<\/a><\/p>\n<h3>Flask\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>pip\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npip install flask\r\n<\/pre>\n<h3>Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u4f5c\u6210<\/h3>\n<p>\u305d\u308c\u3067\u306fWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u65e9\u901f\u4f5c\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><strong>run.py<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef hello_world():\r\n    return '&lt;html&gt;&lt;body&gt;&lt;h1&gt;sample&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;'\r\n\r\nif __name__ == '__main__':\r\n    app.run()\r\n\r\n<\/pre>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npython run.py\r\n<\/pre>\n<p>http:\/\/127.0.0.1:5000\/<\/p>\n<p>\u4ee5\u4e0b\u306e\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u308b\u3068\u6210\u529f\u3067\u3059\u3002<br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_sample.png\"><img loading=\"lazy\" src=\"http:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_sample.png\" alt=\"\" width=\"435\" height=\"313\" class=\"alignnone size-full wp-image-1634\" srcset=\"https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_sample.png 435w, https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_sample-300x216.png 300w\" sizes=\"(max-width: 435px) 100vw, 435px\" \/><\/a><\/p>\n<p>\u4ee5\u524d\u306fPython\u30b3\u30de\u30f3\u30c9\u304b\u3089\u5b9f\u884c\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u6700\u8fd1\u306f\u74b0\u5883\u5909\u6570\u3067\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u5b9a\u3057\u3066flask\u30b3\u30de\u30f3\u30c9\u304b\u3089\u8d77\u52d5\u3059\u308b\u65b9\u6cd5\u304c\u4e00\u822c\u7684\u3067\u3059\u3002Unix\u7cfb\u306e\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n$ export FLASK_APP=run.py\r\n$ flask run\r\n<\/pre>\n<h3>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e\u4f7f\u7528<\/h3>\n<p>\u307e\u305f\u3001flask\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067jinja2\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u304c\u5229\u7528\u53ef\u80fd\u3067\u3059\u3002(jinja2\u306e\u4f7f\u3044\u65b9\u306f<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/760\">\u3053\u3061\u3089<\/a>\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002)<\/p>\n<p>run.py\u3092\u914d\u7f6e\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e0b\u306btemplates\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u3001\u305d\u306e\u76f4\u4e0b\u306bindex.html\u3068\u3044\u3046html\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>.\/templates\/index.html<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!doctype html&gt;\r\n&lt;title&gt;\u30b5\u30f3\u30d7\u30eb&lt;\/title&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;&lt;\/title&gt;\r\n    &lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.2.1.slim.min.js&quot;\r\n            integrity=&quot;sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr\/rE9\/Qpg6aAZGJwFDMVNA\/GpGFF93hXpG5KkN&quot;\r\n            crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.12.9\/umd\/popper.min.js&quot;\r\n            integrity=&quot;sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K\/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q&quot;\r\n            crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\r\n    &lt;script src=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/js\/bootstrap.min.js&quot;\r\n            integrity=&quot;sha384-JZR6Spejh4U02d8jOt6vLEHfe\/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl&quot;\r\n            crossorigin=&quot;anonymous&quot;&gt;&lt;\/script&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/css\/bootstrap.min.css&quot;\r\n          integrity=&quot;sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW\/dAiS6JXm&quot; crossorigin=&quot;anonymous&quot;&gt;\r\n    &lt;link rel=&quot;stylesheet&quot; type=text\/css href=&quot;{{ url_for('static', filename='style.css') }}&quot;&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n    &lt;header&gt;\r\n        &lt;div class=&quot;navbar navbar-dark bg-dark box-shadow&quot;&gt;\r\n            &lt;div class=&quot;container d-flex justify-content-between&quot;&gt;\r\n                &lt;a href=&quot;\/&quot; class=&quot;navbar-brand d-flex align-items-center&quot;&gt;\r\n                    &lt;strong&gt;\u30b5\u30f3\u30d7\u30eb&lt;\/strong&gt;\r\n                &lt;\/a&gt;\r\n            &lt;\/div&gt;\r\n        &lt;\/div&gt;\r\n    &lt;\/header&gt;\r\n    &lt;div class=&quot;content container&quot;&gt;\r\n        &lt;h2&gt;\u5024\u306e\u8868\u793a&lt;\/h2&gt;\r\n        &lt;p&gt;\u5024\uff11\uff1a{{ values.val1 }}&lt;\/p&gt;\r\n        &lt;p&gt;\u5024\uff12\uff1a{{ values.val2 }}&lt;\/p&gt;\r\n    &lt;\/div&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>.\/run.py\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom flask import Flask\r\nfrom flask import render_template\r\n\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef hello_world():\r\n    values = {&quot;val1&quot;: 100, &quot;val2&quot; :200}\r\n    return render_template('index.html', values=values)\r\n\r\nif __name__ == '__main__':\r\n    app.run()\r\n\r\n<\/pre>\n<p>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306ftemplates\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u914d\u7f6e\u3057\u307e\u3059\u3002render_template\u30e1\u30bd\u30c3\u30c9\u306e\u7b2c\uff11\u5f15\u6570\u306b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u7b2c\uff12\u5f15\u6570\u306b\u8f9e\u66f8\u3092\u6307\u5b9a\u3059\u308b\u3068\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5074\u3067\u8f9e\u66f8\u306e\u5024\u3092\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4e0a\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u5148\u307b\u3069\u306eURL\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_templete.png\"><img loading=\"lazy\" src=\"http:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_templete.png\" alt=\"\" width=\"1261\" height=\"743\" class=\"alignnone size-full wp-image-1660\" srcset=\"https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_templete.png 1261w, https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_templete-300x177.png 300w, https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_templete-768x453.png 768w, https:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_templete-1024x603.png 1024w\" sizes=\"(max-width: 1261px) 100vw, 1261px\" \/><\/a>\n<p>\u52d5\u7684\u306aWeb\u753b\u9762\u3092\u7c21\u5358\u306b\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u3067\u306f\u5192\u982d\u306b\u66f8\u3044\u305f\u3068\u304a\u308a\u3001\u6b21\u56de\u4ee5\u964d\u306fflask\u3092\u5229\u7528\u3057\u3066\u7c21\u5358\u306aWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1640\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb1<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1645\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb2 \u5b66\u7fd2\u74b0\u5883\u306e\u69cb\u7bc9<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1647\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb3 mock\u306e\u4f5c\u6210<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1649\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb4 \u5206\u6790\u51e6\u7406<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1651\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb5 \u53c2\u7167\u753b\u9762\u306e\u4f5c\u6210<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1653\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb6 \u4e00\u89a7\u753b\u9762\u306e\u4f5c\u6210<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1707\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb7 flash\u30e1\u30c3\u30bb\u30fc\u30b8<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/1710\">Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb \u88dc\u8db3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flask\u3068\u306f Python\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u3044\u3048\u3070Django\u304c\u6709\u540d\u3067\u3059\u304c\u3001\u8efd\u91cf\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u306f\u6700\u8fd1Flask\u306e\u4eba\u6c17\u304c\u9ad8\u307e\u3063\u3066\u3044\u307e\u3059\u3002 \u975e\u5e38\u306b\u8584\u304f\u30de\u30a4\u30af\u30ed\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u8a00\u308f\u308c\u308b\u3053\u3068\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u305d\u306e\u5206\u5b66\u7fd2\u30b3...<\/p>\n","protected":false},"author":1,"featured_media":1713,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,9],"tags":[171],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1630"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=1630"}],"version-history":[{"count":18,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1630\/revisions"}],"predecessor-version":[{"id":1966,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1630\/revisions\/1966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/1713"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=1630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=1630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=1630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}