{"id":1649,"date":"2018-03-23T21:13:52","date_gmt":"2018-03-23T12:13:52","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=1649"},"modified":"2019-04-14T23:02:59","modified_gmt":"2019-04-14T14:02:59","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%ab4-%e5%88%86%e6%9e%90%e5%87%a6%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/1649","title":{"rendered":"Flask\u3067\u4f5c\u308b\u7c21\u6613\u5206\u6790\u30c4\u30fc\u30eb4 \u5206\u6790\u51e6\u7406"},"content":{"rendered":"<p>\u3053\u306e\u30da\u30fc\u30b8\u3067\u306fWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u4e2d\u6838\u3068\u306a\u308b\u5206\u6790\u51e6\u7406\u306e\u5b9f\u88c5\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<h2>DB\u63a5\u7d9a\u6a5f\u80fd\u306e\u8ffd\u52a0<\/h2>\n<p>\u307e\u305a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u3068\u305d\u306e\u7ba1\u7406\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u307e\u3057\u3087\u3046\u3002\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u3092\u7ba1\u7406\u3059\u308b\u305f\u3081\u306b\u3001run.py\u3067flask\u304b\u3089\u306eimport\u6587\u306bsqlite3\u53ca\u3073g\u3068\u3044\u3046\u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u3088\u3073\u3060\u3059\u305f\u3081\u306bmodel\u3082\u8ffd\u52a0\u3057\u307e\u3059\u3002<font color=\"red\">2019\/4\/14 model\u3001sqlite3\u306eimport\u6587\u304c\u6f0f\u308c\u3066\u3044\u305f\u305f\u3081\u8ffd\u8a18\u3002(\u3054\u6307\u6458\u304f\u3060\u3055\u3063\u305f\u65b9\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002)<\/font><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom flask import Flask, redirect, url_for, render_template, request, g\r\nimport sqlite3\r\nimport models\r\n<\/pre>\n<p>\u307e\u305f\u3001\u4ee5\u4e0b\uff13\u3064\u3001connect_db\u3001get_db\u3001close_db\u95a2\u6570\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n# \u4ee5\u4e0b\u3001DB\u63a5\u7d9a\u95a2\u9023\u306e\u95a2\u6570\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# \u4ee5\u4e0b\u3001\u753b\u9762\/\u6a5f\u80fd\u6bce\u306e\u95a2\u6570\r\n@app.route('\/')\r\ndef index():\r\n    &quot;&quot;&quot; \u4e00\u89a7\u753b\u9762 &quot;&quot;&quot;\r\n   # \u4ee5\u4e0b\u7701\u7565\r\n   # :\r\n<\/pre>\n<p>\u65b0\u305f\u306b\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u305fg\u3068\u3044\u3046\u5909\u6570\u3067\u3059\u304c\u3001\u3053\u308c\u306f\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3067\u3059\u3002g.\u4efb\u610f\u306e\u5909\u6570\u540d\u3067\u4ee3\u5165\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u4e0a\u306e\u4f8b\u3067\u306fg.sqlite_db = connect_db()\u3067\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092g\u306b\u683c\u7d0d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>connect_db\u95a2\u6570\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u63a5\u7d9a\u3057\u3001\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u8fd4\u3057\u307e\u3059\u3002get_db\u306f\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u53d6\u5f97\u3059\u308b\u95a2\u6570\u3067\u3059\u3002g\u306b\u683c\u7d0d\u3055\u308c\u3066\u3044\u308c\u3070\u305d\u308c\u3092\u4f7f\u7528\u3057\u3001\u306a\u3051\u308c\u3070connect_db\u3092\u547c\u3073\u51fa\u3057\u3066\u65b0\u305f\u306b\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u6700\u5f8c\u306eclose_db\u306f\u3001\u51e6\u7406\u7d42\u4e86\u6642\u306b\u547c\u3073\u51fa\u3055\u308c\u3001\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u304c\u3042\u308c\u3070\u30af\u30ed\u30fc\u30ba\u3057\u307e\u3059\u3002<\/p>\n<p>close_db\u306b\u306f@app.teardown_appcontext\u30c7\u30b3\u30ec\u30fc\u30bf\u304c\u3064\u3051\u3089\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u306e\u30c7\u30b3\u30ec\u30fc\u30bf\u3092\u3064\u3051\u3053\u3068\u306b\u3088\u308a\u30ea\u30af\u30a8\u30b9\u30c8\u7d42\u4e86\u6642\u306b\u81ea\u52d5\u7684\u306b\u305d\u306e\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u306e\u4fee\u6b63<\/h2>\n<h3>\u5206\u6790\u51e6\u7406\u306e\u8ffd\u52a0<\/h3>\n<p>\u3067\u306f\u3001\u30e1\u30a4\u30f3\u306e\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u3092\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002\u4eca\u56de\u4f5c\u6210\u3059\u308bWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001textarea\u306b\u8cbc\u308a\u4ed8\u3051\u305fcsv\u3082\u3057\u304f\u306ftsv\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3001matplotlib\u3067\u6563\u5e03\u56f3\u884c\u5217\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\nmodels.py\u306b\u4ee5\u4e0b\u3092\u30da\u30fc\u30b9\u30c8\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\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    &quot;&quot;&quot;\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<\/pre>\n<p>data\u306fcsv\u304btsv\u306e\u6587\u5b57\u5217\u3068\u3057\u307e\u3059\u3002\u4e0a\u306e\u30b3\u30fc\u30c9\u306e\uff11\uff13\u884c\u76ee\u3001\uff11\uff14\u884c\u76ee\u3067\u306f\u305d\u306eCSV\u3001TSV\u6587\u5b57\u5217\u304b\u3089\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u5f8c\u3001\u751f\u6210\u3057\u305f\u30c7\u30fc\u30bf\u30d5\u30ec\u30fc\u30e0\u3092\u5143\u306b\u6563\u5e03\u56f3\u884c\u5217\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002\u4f5c\u6210\u3057\u305f\u6563\u5e03\u56f3\u884c\u5217\u306f\u4e00\u65e6\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u4fdd\u5b58\u3057\u3066\u51e6\u7406\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002<\/p>\n<p>\u4fdd\u5b58\u5148\u3067\u3059\u304c\u3001\u5f8c\u307b\u3069\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u4e0a\u3067\u53c2\u7167\u3059\u308b\u305f\u3081\u306b\u9759\u7684\u30d5\u30a1\u30a4\u30eb\u914d\u7f6e\u7528\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002<\/p>\n<h3>\u633f\u5165\u51e6\u7406\u306e\u8ffd\u52a0<\/h3>\n<p>\u6b21\u306b\u3001\u30c7\u30fc\u30bf\u306e\u767b\u9332\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>models.py\u306b\u4ee5\u4e0b\u3092\u8ffd\u8a18\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\ndef insert(con, title, data, img):\r\n    &quot;&quot;&quot; INSERT\u51e6\u7406 &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<\/pre>\n<p>\u5f15\u6570\u3067\u6307\u5b9a\u3055\u308c\u305fDB\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3068\u633f\u5165\u3059\u308b\u30c7\u30fc\u30bf\u3092\u3082\u3068\u306b\u633f\u5165\u51e6\u7406\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<p>run.py\u3067\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u30b0\u30ed\u30fc\u30d0\u30eb\u9818\u57df\u306b\u683c\u7d0d\u3057\u305f\u305f\u3081\u3001\u5f15\u6570\u3067\u306f\u306a\u304f\u305d\u3053\u304b\u3089\u53d6\u5f97\u3057\u3066\u3082\u69cb\u3044\u307e\u305b\u3093\u3002<\/p>\n<p>\u305f\u3060\u3057\u3001\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u304cflask\u306b\u4f9d\u5b58\u3057\u3066\u3057\u307e\u3046\u305f\u3081\u3001\u518d\u5229\u7528\u6027\u304c\u4e0b\u304c\u3063\u3066\u3057\u307e\u3046\u70b9\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>run.py\u306e\u4fee\u6b63<\/h2>\n<p>run.py\u306eanalysis\u95a2\u6570\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\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    return redirect(url_for('view', pk=pk))\r\n<\/pre>\n<p>request.form\u3067html\u306e\u30d5\u30a9\u30fc\u30e0\u304b\u3089\u9001\u4fe1\u3055\u308c\u305f\u5024\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002models.create_scatter\u3067\u6563\u5e03\u56f3\u884c\u5217\u3092\u751f\u6210\u3057\u305f\u5f8c\u3001\u9001\u4fe1\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u767b\u9332\u3057\u307e\u3059\u3002<\/p>\n<p>\u6700\u5f8c\u306b\u53c2\u7167\u753b\u9762\u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3057\u307e\u3059\u3002url_for\u3067URL\u3068\u5f15\u6570\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>\u52d5\u4f5c\u78ba\u8a8d<\/h2>\n<p>\u6700\u5f8c\u306b\u52d5\u4f5c\u78ba\u8a8d\u3092\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u4ee5\u4e0b\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u753b\u9762\u3092\u8868\u793a\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\nhttp:\/\/localhost:5000\/create<\/p>\n<p>\u30bf\u30a4\u30c8\u30eb\u306b\u9069\u5f53\u306a\u6587\u5b57\u5217\u3001\u30c7\u30fc\u30bf\u306b\u4ee5\u4e0b\u306e\u6587\u5b57\u5217\u3092\u8cbc\u308a\u4ed8\u3051\u3066\u9001\u4fe1\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ncol1,col2,col3\r\n100,200,300\r\n300,400,100\r\n200,100,100\r\n<\/pre>\n<p>\u30a8\u30e9\u30fc\u304c\u767a\u751f\u305b\u305a\u306b\u53c2\u7167\u753b\u9762\u306emock\u306b\u9077\u79fb\u3059\u308b\u3068\u6210\u529f\u3067\u3059\u3002<br \/>\n\u307e\u305f\u3001static\/result\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u914d\u4e0b\u306b\u753b\u50cf\u304c\u751f\u6210\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3001results\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u304c\u633f\u5165\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u30da\u30fc\u30b8\u3067\u306fWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u4e2d\u6838\u3068\u306a\u308b\u5206\u6790\u51e6\u7406\u306e\u5b9f\u88c5\u3092\u884c\u3044\u307e\u3059\u3002 DB\u63a5\u7d9a\u6a5f\u80fd\u306e\u8ffd\u52a0 \u307e\u305a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u3068\u305d\u306e\u7ba1\u7406\u6a5f\u80fd\u3092\u5b9f\u88c5\u3057\u307e\u3057\u3087\u3046\u3002\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u63a5\u7d9a\u3092\u7ba1\u7406\u3059\u308b\u305f\u3081\u306b\u3001run.py\u3067flask\u304b\u3089\u306ei...<\/p>\n","protected":false},"author":1,"featured_media":1717,"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\/1649"}],"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=1649"}],"version-history":[{"count":13,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1649\/revisions"}],"predecessor-version":[{"id":2045,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/1649\/revisions\/2045"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/1717"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=1649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=1649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=1649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}