{"id":1645,"date":"2018-03-20T19:43:42","date_gmt":"2018-03-20T10:43:42","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=1645"},"modified":"2018-11-04T22:36:54","modified_gmt":"2018-11-04T13:36:54","slug":"flask%e3%81%a7%e4%bd%9c%e3%82%8b%e7%b0%a1%e6%98%93%e5%88%86%e6%9e%90%e3%83%84%e3%83%bc%e3%83%ab2-%e5%ad%a6%e7%bf%92%e7%92%b0%e5%a2%83%e3%81%ae%e6%a7%8b%e7%af%89","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/1645","title":{"rendered":"Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb2 \u5b66\u7fd2\u74b0\u5883\u306e\u69cb\u7bc9"},"content":{"rendered":"<p>\u3053\u3053\u3067\u306f\u5206\u6790\u30c4\u30fc\u30eb\u306e\u96db\u5f62\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u74b0\u5883\u69cb\u7bc9<\/h2>\n<h3>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u69cb\u6210<\/h3>\n<p>\u9069\u5f53\u306a\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e0b\u306b\u3001\u4ee5\u4e0b\u306ezip\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u89e3\u51cd\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n<font color=\"red\">2018\/4\/21\u8ffd\u8a18\u3000schema.sql\u304c\u6f0f\u308c\u3066\u304a\u308a\u307e\u3057\u305f\u3002\u4e0b\u8a18\u30ea\u30f3\u30af\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u304f\u3060\u3055\u3044\u3002(\u3054\u6307\u6458\u304f\u3060\u3055\u3063\u305f\u65b9\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002)<\/font><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/wp-content\/uploads\/2018\/03\/flask_init_sample-1.zip\">flask_init_sample<\/a><\/p>\n<p>\u89e3\u51cd\u3059\u308b\u3068\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u96db\u5f62\u304c\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3068\u306a\u308a\u307e\u3059\u3002Python\u30d5\u30a1\u30a4\u30eb\u304c\u305f\u3063\u305f\uff12\u3064\u3057\u304b\u306a\u3044\u3053\u3068\u306b\u7740\u76ee\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nflask_sample\r\n\u251c\u2500\u2500 models.py\r\n\u251c\u2500\u2500 requirements.txt\r\n\u251c\u2500\u2500 run.py\r\n\u251c\u2500\u2500 schema.sql\r\n\u251c\u2500\u2500 static\r\n\u2502\u00a0\u00a0 \u251c\u2500\u2500 result\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 style.css\r\n\u2514\u2500\u2500 templates\r\n    \u251c\u2500\u2500 base.html\r\n    \u251c\u2500\u2500 edit.html\r\n    \u251c\u2500\u2500 index.html\r\n    \u2514\u2500\u2500 view.html\r\n\r\n<\/pre>\n<h3>\u30d5\u30a1\u30a4\u30eb\u3001\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u89e3\u8aac<\/h3>\n<p>\u7c21\u5358\u306b\u3067\u3059\u304c\u3001\u30d5\u30a1\u30a4\u30eb\u3068\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u8aac\u660e\u3092\u3057\u307e\u3059\u3002<\/p>\n<h4>run.py<\/h4>\n<p>Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u8d77\u52d5\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3059\u3002URL\u3054\u3068\u306b\u51e6\u7406\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u884c\u3044\u307e\u3059\u3002Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u3057\u3066flask\u304c\u95a2\u308f\u308b\u306e\u306f\u3053\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u307f\u3068\u306a\u308a\u307e\u3059\u3002\u30da\u30fc\u30b8\u4e0b\u90e8\u3067\u3082\u3046\u5c11\u3005\u7d30\u304b\u304f\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<h4>models.py<\/h4>\n<p>\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3059\u3002\u5206\u6790\u51e6\u7406\uff08\u6563\u5e03\u56f3\u884c\u5217\u306e\u751f\u6210\uff09\u3068DB\u30a2\u30af\u30bb\u30b9\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<h4>schema.sql<\/h4>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4>requirements.txt<\/h4>\n<p>pip\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5bfe\u8c61\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3059\u3002<\/p>\n<h4>static<\/h4>\n<p>css\u306a\u3069\u306e\u9759\u7684\u30d5\u30a1\u30a4\u30eb\u3092\u914d\u7f6e\u3059\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u3059\u3002\u307e\u305f\u3001\u914d\u4e0b\u306eresult\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u306f\u5206\u6790\u3067\u4f5c\u6210\u3057\u305f\u30b0\u30e9\u30d5\u306e\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3057\u307e\u3059\u3002<\/p>\n<h4>templates<\/h4>\n<p>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\uff08html\u306e\u96db\u5f62\uff09\u3092\u914d\u7f6e\u3059\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u3059\u3002\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306f\u89aa\u5b50\u95a2\u4fc2\u304c\u3042\u308a\u3001base.html\u304c\u5171\u901a\u3068\u306a\u308b\u89aa\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3001\u305d\u308c\u4ee5\u5916\u304c\u5b50\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u5fc5\u8981\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>matplotlib\u3092\u4f7f\u7528\u3057\u307e\u3059\u306e\u3067\u3001Ubunt\u306e\u65b9\u306f\u4ee5\u4e0b\u3067Tkinter\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsudo apt install python-tk tk-dev\r\n<\/pre>\n<p>pip\u3067\u5fc5\u8981\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\npip install -r requirements.txt\r\n<\/pre>\n<p>\u4f7f\u7528\u3059\u308b\u4e3b\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>flask<\/li>\n<li>pandas<\/li>\n<li>matplotlib<\/li>\n<li>scipy<\/li>\n<\/ul>\n<h3>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u69cb\u7bc9<\/h3>\n<p>sqlite3\u30b3\u30de\u30f3\u30c9\u304c\u4f7f\u7528\u3067\u304d\u308b\u3053\u3068\u3092\u524d\u63d0\u3068\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001sqlite3\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nsqlite3 db.sqlite3 &lt; schema.sql\r\n<\/pre>\n<p>\u30c6\u30fc\u30d6\u30eb\u30ec\u30a4\u30a2\u30a6\u30c8\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u30ab\u30e9\u30e0<\/th>\n<th>\u578b<\/th>\n<th>\u5185\u5bb9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>integer<\/td>\n<td>\u4e3b\u30ad\u30fc<\/td>\n<\/tr>\n<tr>\n<td>title<\/td>\n<td>text<\/td>\n<td>\u30bf\u30a4\u30c8\u30eb<\/td>\n<\/tr>\n<tr>\n<td>data<\/td>\n<td>text<\/td>\n<td>\u5206\u6790\u5bfe\u8c61\u3068\u306a\u308b\u30c7\u30fc\u30bf\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>img<\/td>\n<td>text<\/td>\n<td>\u753b\u50cf\u51fa\u529b\u30d1\u30b9<\/td>\n<\/tr>\n<tr>\n<td>created<\/td>\n<td>datetime<\/td>\n<td>\u4f5c\u6210\u65e5\u6642<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>flask\u3068URL\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0<\/h2>\n<p>Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u4e3b\u8ef8\u3068\u306a\u308brun.py\u306b\u3064\u3044\u3066\u3082\u3046\u5c11\u3057\u8aac\u660e\u3055\u305b\u3066\u304f\u3060\u3055\u3044\u3002\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nfrom flask import Flask, redirect, url_for, render_template\r\n\r\napp = Flask(__name__)\r\napp.config.from_object(__name__)\r\n\r\napp.config.update(dict(\r\n    DATABASE=os.path.join(app.root_path, 'db.sqlite3'),\r\n    SECRET_KEY='foo-baa',\r\n))\r\n\r\n\r\n@app.route('\/')\r\ndef index():\r\n    &quot;&quot;&quot; \u4e00\u89a7\u753b\u9762 &quot;&quot;&quot;\r\n    return render_template('index.html', results={})\r\n\r\n\r\n@app.route('\/create')\r\ndef create():\r\n    &quot;&quot;&quot; \u65b0\u898f\u4f5c\u6210\u753b\u9762 &quot;&quot;&quot;\r\n    return render_template('edit.html')\r\n\r\n\r\n@app.route('\/analysis', methods=['POST'])\r\ndef analysis():\r\n    &quot;&quot;&quot; \u5206\u6790\u5b9f\u884c\u51e6\u7406 &quot;&quot;&quot;\r\n    return redirect(url_for('view', pk=0))\r\n\r\n\r\n@app.route('\/delete\/&lt;pk&gt;')\r\ndef delete(pk):\r\n    &quot;&quot;&quot; \u7d50\u679c\u524a\u9664\u51e6\u7406 &quot;&quot;&quot;\r\n    return redirect(url_for('index'))\r\n\r\n\r\n@app.route('\/view\/&lt;pk&gt;')\r\ndef view(pk):\r\n    &quot;&quot;&quot; \u7d50\u679c\u53c2\u7167\u51e6\u7406 &quot;&quot;&quot;\r\n    return render_template('view.html', result={})\r\n\r\n\r\nif __name__ == '__main__':\r\n    app.run()\r\n\r\n<\/pre>\n<p>app.config.update\u3067flask\u306e\u8a2d\u5b9a\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u60c5\u5831\u3068\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u5229\u7528\u3059\u308b\u9375\u306e\u5b9a\u6570\u8a2d\u5b9a\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\uff11\uff12\u884c\u76ee\u4ee5\u4e0b\u306eindex\u3001create\u3001analysis\u3001delete\u3001view\u306fWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u753b\u9762\u3084\u51e6\u7406\u306b\u5bfe\u5fdc\u3059\u308b\u95a2\u6570\u3067\u3059\u3002<\/p>\n<p>@app.route\u30c7\u30b3\u30ec\u30fc\u30bf\u3067URL\u3068\u95a2\u6570\u306e\u3072\u3082\u4ed8\u3051\u306b\u3088\u308a\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u672c\u8b1b\u5ea7\u3067\u306eURL\u3068\u95a2\u6570\u306e\u5bfe\u5fdc\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>URL<\/th>\n<th>\u95a2\u6570<\/th>\n<th>\u753b\u9762\/\u6a5f\u80fd<\/th>\n<th>\u30e1\u30bd\u30c3\u30c9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\/<\/td>\n<td>index<\/td>\n<td>Top\u753b\u9762\uff08\u7d50\u679c\u4e00\u89a7\uff09<\/td>\n<td>GET\/POST<\/td>\n<\/tr>\n<tr>\n<td>\/create<\/td>\n<td>create<\/td>\n<td>\u65b0\u898f\u5206\u6790\u4f5c\u6210\u753b\u9762<\/td>\n<td>GET\/POST<\/td>\n<\/tr>\n<tr>\n<td>\/analysis<\/td>\n<td>analysis<\/td>\n<td>\u5206\u6790\u51e6\u7406<\/td>\n<td>POST<\/td>\n<\/tr>\n<tr>\n<td>\/delete\/<pk><\/td>\n<td>delete<\/td>\n<td>\u524a\u9664\u51e6\u7406<\/td>\n<td>POST<\/td>\n<\/tr>\n<tr>\n<td>\/view\/<pk><\/td>\n<td>vies<\/td>\n<td>\u53c2\u7167\u51e6\u7406<\/td>\n<td>GET\/POST<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>app.route\u306e\u7b2c\uff12\u5f15\u6570\u3067\u30e1\u30bd\u30c3\u30c9\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u307e\u305f\u3001URL\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u52a0\u3048\u305f\u3044\u5834\u5408\u306f37\u884c\u76ee\u306e\u3088\u3046\u306b&lt;pk&gt;\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<br \/>\n<\/p>\n<h2>\u88dc\u8db3<\/h2>\n<p>sqlite3\u3068jinja2\u306b\u95a2\u3059\u308b\u5185\u5bb9\u304c\u51fa\u3066\u304d\u307e\u3059\u3002\u5b66\u7fd2\u3092\u3059\u3059\u3081\u308b\u4e0a\u3067\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u4ee5\u4e0b\u30ea\u30f3\u30af\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002\uff08\u5fc5\u305a\u3057\u3082\u524d\u3082\u3063\u3066\u5b66\u7fd2\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\u3002\uff09<\/p>\n<p><a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/745\">sqlite3<\/a><br \/>\n<a href=\"http:\/\/www.python.ambitious-engineer.com\/archives\/760\">jinja2\u5165\u9580 \u305d\u306e1<\/a><\/p>\n<p>\u6b21\u56de\u3001mock\u3092\u4f5c\u6210\u3057\u3066\u307e\u305a\u52d5\u304f\u3082\u306e\u3092\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3053\u3067\u306f\u5206\u6790\u30c4\u30fc\u30eb\u306e\u96db\u5f62\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u74b0\u5883\u3092\u69cb\u7bc9\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002 \u74b0\u5883\u69cb\u7bc9 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u69cb\u6210 \u9069\u5f53\u306a\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e0b\u306b\u3001\u4ee5\u4e0b\u306ezip\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u89e3\u51cd\u3057\u3066\u304f\u3060\u3055\u3044\u3002 2018\/4\/21\u8ffd\u8a18\u3000schema.s...<\/p>\n","protected":false},"author":1,"featured_media":1715,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[153,3,9],"tags":[171,170],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1645"}],"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=1645"}],"version-history":[{"count":17,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1645\/revisions"}],"predecessor-version":[{"id":1968,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1645\/revisions\/1968"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/1715"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=1645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=1645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=1645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}