{"id":1710,"date":"2018-03-30T14:19:41","date_gmt":"2018-03-30T05:19:41","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=1710"},"modified":"2018-11-04T22:38:25","modified_gmt":"2018-11-04T13:38:25","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%ab-%e8%a3%9c%e8%b6%b3","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/1710","title":{"rendered":"Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb \u88dc\u8db3"},"content":{"rendered":"<p>\u88dc\u8db3\u3068\u3057\u3066\u5b8c\u6210\u5f8c\u306e\u69cb\u6210\u3068\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u63b2\u8f09\u3057\u307e\u3059\u3002<\/p>\n<h2>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8<\/h2>\n<p>\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n.\r\n\u251c\u2500\u2500 db.sqlite3\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>schema.sql<\/h3>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\ndrop table if exists results;\r\ncreate table results (\r\n  `id` integer primary key autoincrement,\r\n  `title` text not null,\r\n  `data` text not null,\r\n  `img` text not null,\r\n  `created` datetime default CURRENT_TIMESTAMP\r\n);\r\n<\/pre>\n<h3>requirements.txt<\/h3>\n<p>\u5229\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002(2018\/4\/21 \u4fee\u6b63 scipy\u3092\u8ffd\u8a18\u3057\u307e\u3057\u305f\u3002)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nclick==6.7\r\ncycler==0.10.0\r\nFlask==0.12.2\r\nitsdangerous==0.24\r\nJinja2==2.10\r\nkiwisolver==1.0.1\r\nMarkupSafe==1.0\r\nmatplotlib==2.2.0\r\nnumpy==1.14.1\r\npandas==0.22.0\r\npyparsing==2.2.0\r\npython-dateutil==2.6.1\r\npytz==2018.3\r\nscipy==1.0.1\r\nsix==1.11.0\r\nWerkzeug==0.14.1\r\n<\/pre>\n<h2>Python\u30b3\u30fc\u30c9<\/h2>\n<h3>run.py<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport sqlite3\r\nfrom flask import Flask, request, g, redirect, url_for, render_template, flash\r\nimport models\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\ndef connect_db():\r\n    &quot;&quot;&quot; \u30c7\u30fc\u30bf\u30d9\u30b9\u63a5\u7d9a\u306b\u63a5\u7d9a\u3057\u307e\u3059 &quot;&quot;&quot;\r\n    con = sqlite3.connect(app.config['DATABASE'])\r\n    con.row_factory = sqlite3.Row\r\n    return con\r\n\r\n\r\ndef get_db():\r\n    &quot;&quot;&quot; connection\u3092\u53d6\u5f97\u3057\u307e\u3059 &quot;&quot;&quot;\r\n    if not hasattr(g, 'sqlite_db'):\r\n        g.sqlite_db = connect_db()\r\n    return g.sqlite_db\r\n\r\n\r\n@app.teardown_appcontext\r\ndef close_db(error):\r\n    &quot;&quot;&quot; db\u63a5\u7d9a\u3092close\u3057\u307e\u3059 &quot;&quot;&quot;\r\n    if hasattr(g, 'sqlite_db'):\r\n        g.sqlite_db.close()\r\n\r\n\r\n# \u4ee5\u4e0b\u3001\u753b\u9762\u306b\u95a2\u308f\u308b\u30e1\u30bd\u30c3\u30c9\r\n\r\n@app.route('\/')\r\ndef index():\r\n    &quot;&quot;&quot; \u4e00\u89a7\u753b\u9762 &quot;&quot;&quot;\r\n    con = get_db()\r\n    results = models.select_all(con)\r\n    return render_template('index.html', results=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\r\n    title = request.form['title']\r\n    data = request.form['data']\r\n    img = models.create_scatter(data)\r\n\r\n    con = get_db()\r\n\r\n    pk = models.insert(con, title, data, img)\r\n    flash('\u767b\u9332\u51e6\u7406\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002')\r\n    return redirect(url_for('view', pk=pk))\r\n\r\n\r\n@app.route('\/delete\/&lt;pk&gt;', methods=['POST'])\r\ndef delete(pk):\r\n    &quot;&quot;&quot; \u7d50\u679c\u524a\u9664\u51e6\u7406 &quot;&quot;&quot;\r\n    con = get_db()\r\n    models.delete(con, pk)\r\n    flash('\u524a\u9664\u51e6\u7406\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002')\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    con = get_db()\r\n    result = models.select(con, pk)\r\n    return render_template('view.html', result=result)\r\n\r\n\r\nif __name__ == '__main__':\r\n    app.run()\r\n\r\n<\/pre>\n<h3>models.py<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&quot;&quot;&quot;\r\n\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u30e2\u30b8\u30e5\u30fc\u30eb\r\n&quot;&quot;&quot;\r\nfrom matplotlib import pyplot as plt\r\nfrom pandas.plotting import scatter_matrix\r\nimport pandas as pd\r\nimport time\r\nimport io\r\n\r\n\r\ndef create_scatter(data):\r\n    data = data.replace(',', '\\t').replace(' ', '\\t')\r\n    df = pd.read_csv(io.StringIO(data), sep='\\t')\r\n\r\n    # \u30d7\u30ed\u30c3\u30c8\u30de\u30fc\u30ab\u30fc\u306e\u5927\u304d\u3055\u3001\u8272\u3001\u900f\u660e\u5ea6\u3092\u5909\u66f4\r\n    scatter_matrix(df, diagonal='kde', color='#AAAAFF', edgecolors='#0000FF', alpha=0.5)\r\n\r\n    # \u30d5\u30a1\u30a4\u30eb\u540d\r\n    filename = time.strftime('%Y%m%d%H%M%S') + &quot;.png&quot;\r\n\r\n    # \u4fdd\u5b58\u5148\u306e\u30d1\u30b9\r\n    save_path = &quot;.\/static\/result\/&quot; + filename\r\n\r\n    # \u8868\u793a\u7528URL\r\n    url = &quot;result\/&quot; + filename\r\n\r\n    # \u4fdd\u5b58\u51e6\u7406\u3092\u884c\u3046\r\n    plt.savefig(save_path)\r\n\r\n    # plt\u3092close\r\n    plt.close()\r\n\r\n    return url\r\n\r\n\r\ndef select_all(con):\r\n    &quot;&quot;&quot; SELECT\u3059\u308b &quot;&quot;&quot;\r\n    cur = con.execute('select id, title, data, img, created from results order by id desc')\r\n    return cur.fetchall()\r\n\r\n\r\ndef select(con, pk):\r\n    &quot;&quot;&quot; \u6307\u5b9a\u3057\u305f\u30ad\u30fc\u306e\u30c7\u30fc\u30bf\u3092SELECT\u3059\u308b &quot;&quot;&quot;\r\n    cur = con.execute('select id, title, data, img, created from results where id=?', (pk,))\r\n    return cur.fetchone()\r\n\r\n\r\ndef insert(con, title, data, img):\r\n    &quot;&quot;&quot; INSERT\u3059\u308b &quot;&quot;&quot;\r\n    cur = con.cursor()\r\n    cur.execute('insert into results (title, data, img) values (?, ?, ?)', [title, data, img])\r\n\r\n    pk = cur.lastrowid\r\n    con.commit()\r\n\r\n    return pk\r\n\r\n\r\ndef delete(con, pk):\r\n    &quot;&quot;&quot; \u6307\u5b9a\u3057\u305f\u30ad\u30fc\u306e\u30c7\u30fc\u30bf\u3092DELETE\u3059\u308b &quot;&quot;&quot;\r\n    cur = con.cursor()\r\n    cur.execute('delete from results where id=?', (pk,))\r\n    con.commit()\r\n\r\n<\/pre>\n<h2>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8<\/h2>\n<h3>base.html<\/h3>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!doctype html&gt;\r\n&lt;title&gt;\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb&lt;\/title&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb&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&lt;body&gt;\r\n\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;\u7c21\u6613\u5206\u6790\u30c4\u30fc\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\r\n&lt;div class=&quot;container&quot;&gt;\r\n    {% for message in get_flashed_messages() %}\r\n    &lt;div class=&quot;alert alert-success&quot;&gt;{{ message }}&lt;\/div&gt;\r\n    {% endfor %} {% block body %}{% endblock %}\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<h3>edit.html<\/h3>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n{% extends &quot;base.html&quot; %}\r\n{% block body %}\r\n&lt;h1&gt;\u65b0\u898f\u5206\u6790&lt;\/h1&gt;\r\n&lt;form action=&quot;{{ url_for('analysis') }}&quot; method=&quot;post&quot;&gt;\r\n\r\n    &lt;label&gt;\u30bf\u30a4\u30c8\u30eb&lt;\/label&gt;\r\n    &lt;input class=&quot;form-control&quot; type=&quot;text&quot; size=&quot;30&quot; name=&quot;title&quot;&gt;\r\n    &lt;label&gt;\u5206\u6790\u30c7\u30fc\u30bf&lt;\/label&gt;\r\n    &lt;textarea class=&quot;form-control&quot; name=&quot;data&quot; rows=&quot;5&quot;&gt;&lt;\/textarea&gt;\r\n    &lt;input class=&quot;btn btn-primary&quot; type=&quot;submit&quot; value=&quot;\u9001\u4fe1&quot;&gt;\r\n\r\n&lt;\/form&gt;\r\n{% endblock %}\r\n<\/pre>\n<h3>index.html<\/h3>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n{% extends &quot;base.html&quot; %}\r\n{% block body %}\r\n&lt;h1&gt;\u5206\u6790\u4e00\u89a7&lt;\/h1&gt;\r\n&lt;a href=&quot;\/create&quot;&gt;\u65b0\u898f\u5206\u6790&lt;\/a&gt;\r\n&lt;table class=&quot;table table-striped table-hover&quot;&gt;\r\n    &lt;tr&gt;\r\n        &lt;th&gt;id&lt;\/th&gt;\r\n        &lt;th&gt;title&lt;\/th&gt;\r\n        &lt;th&gt;date&lt;\/th&gt;\r\n        &lt;th&gt;\u64cd\u4f5c&lt;\/th&gt;\r\n    &lt;\/tr&gt;\r\n    {% for result in results %}\r\n    &lt;tr&gt;\r\n        &lt;td&gt;{{ result.id }}&lt;\/td&gt;\r\n        &lt;td&gt;{{ result.title|safe}}&lt;\/td&gt;\r\n        &lt;td&gt;{{ result.created }}&lt;\/td&gt;\r\n        &lt;td&gt;\r\n            &lt;a href=&quot;\/view\/{{ result.id }}&quot;&gt;&lt;button class=&quot;btn btn-primary&quot;&gt;\u53c2\u7167&lt;\/button&gt;&lt;\/a&gt;\r\n            &lt;form action=&quot;\/delete\/{{ result.id }}&quot; style=&quot;display: inline&quot; method=&quot;post&quot;&gt;\r\n                &lt;input class=&quot;btn btn-danger&quot; type=&quot;submit&quot; value=&quot;\u524a\u9664&quot; onclick='return confirm(&quot;\u524a\u9664\u3057\u307e\u3059\u304c\u3088\u308d\u3057\u3044\u3067\u3059\u304b\uff1f&quot;)';&gt;\r\n            &lt;\/form&gt;\r\n\r\n        &lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n    {% endfor %}\r\n\r\n&lt;\/table&gt;\r\n\r\n{% endblock %}\r\n<\/pre>\n<p><font color=\"red\">2018\/4\/27 index.html\u306edelete\u90e8\u5206\u306b\u8aa4\u8a18\u304c\u3042\u3063\u305f\u305f\u3081\u4fee\u6b63\u3057\u307e\u3057\u305f\u3002(\u3054\u6307\u6458\u304f\u3060\u3055\u3063\u305f\u65b9\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002)<\/font><\/p>\n<h3>view.html<\/h3>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n{% extends &quot;base.html&quot; %}\r\n{% block body %}\r\n&lt;h1&gt;\u7d50\u679c\u53c2\u7167&lt;\/h1&gt;\r\n\r\n&lt;h3&gt;{{ result.id }}:{{ result.title|safe}}&lt;\/h3&gt;\r\n&lt;p&gt;{{ result.created }}&lt;\/p&gt;\r\n&lt;div class=&quot;row&quot;&gt;\r\n    &lt;img src=&quot;{{ url_for('static', filename=result.img) }}&quot;&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div class=&quot;row&quot;&gt;\r\n    &lt;textarea class=&quot;form-control&quot; name=&quot;data&quot; rows=&quot;5&quot;&gt;{{result.data}}&lt;\/textarea&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;br&gt;&lt;br&gt;\r\n{% endblock %}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u88dc\u8db3\u3068\u3057\u3066\u5b8c\u6210\u5f8c\u306e\u69cb\u6210\u3068\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u63b2\u8f09\u3057\u307e\u3059\u3002 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8 \u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3068\u306a\u308a\u307e\u3059\u3002 schema.sql requirements.txt \u5229\u7528\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002(2018\/4\/21 \u4fee\u6b63 ...<\/p>\n","protected":false},"author":1,"featured_media":1721,"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\/1710"}],"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=1710"}],"version-history":[{"count":5,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1710\/revisions"}],"predecessor-version":[{"id":1974,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1710\/revisions\/1974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/1721"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=1710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=1710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=1710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}