ノート
訪問者数 815      最終更新 2019-01-18 (金) 07:57:02

ディレクトリbubbleを作る

python3 -m venv bubble
cd bubble
source bin/activate

flaskDocs)で必要なソフトをインストールする。

pip install flask
pip install flask-sqlalchemy  flask-sqlalchemyとSQLAlchemyが入るらしい
flask-login?

その他に参考: Flask-Loginの使い方

デプロイ用にuwsgiをインストールする

pip install uwsgi

Flaskのためのファイルとディレクトリを作る

manage.py
requirements.txt
bubble/
  |- __init__.py
  |- config.py
  |- views.py
  |- models.py
  |- static/
      |- style.css
  |- templates/
      |- layout.html
      |- show_entries.html

config.py中に、

SQLALCHEMY_DATABASE_URI = 'sqlite:///flaskr.db'
SECRET_KEY = b'------------'
SQLALCHEMY_TRACK_MODIFICATIONS = True

:q! を置くが、SECRET_KEYは

>>> import os
>>> os.urandom(24)

で生成するとよいと書いてある。

models.pyでclass Entryとinit()を作ったところで、データベースを作ってしまう。

>>> from flaskr.models import init
>>> init()

faviconを追加するにはviews.pyの中で、

@app.route('/fabicon.ico')
def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'),
                               'favicon.ico', mimetype='image/vnd.microsoft.icon')

とともに、templates/layout.html中に

<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">

を追加する。

flask単体で起動するには、bubble中で

python manage.py

とする。またuwsgi経由で起動するには、

uwsgi --http :5000 --wsgi-file manage.py --callable app

とする。これをコマンドスクリプトとして作っておくとよい。

Bootstrapを使いたいので、staticの下にbootstrap-4.1.3-dist.zipを展開、css, jsの2つのディレクトリができる。Bootstrapの読み出しは、templates/layout.htmlの中で行う。

<!doctype html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>BubbleCharts</title>
    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    <!-- Bootstrap -->
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    {% block content %}{% endblock %}
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="/static/js/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="/static/js/bootstrap.min.js"></script>

<div class=page>
  <h1>BubbleCharts</h1>
  <div class="container">
    <div class="row">
      <div class="col-md-6"> これは </div>
      <div class="col-md-6"> 困った6 </div>
    </div>
  </div>
  {% for message in get_flashed_messages() %}
    <div class=flash>{{ message }}</div>
  {% endfor %}
  {% block body %}{% endblock %}
</div>

  </body>
</html>

イメージを出力する

イメージをブラウザに出力するにはいくつかのやり方があるらしい(Flaskでファイルダウンロードを実現する3つの方法

実際にやってみると、次の通り。


Python Flask Javascriptで変数の受け渡し


初心者の決定版アプリ: Flask と SQLite で TODO アプリを作る!

ちなみに、プログラムbubbleでのSQLite周りのテストプログラムとして、こんなものを用意した。

# -*- coding: utf-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from bubble import app, db
from bubble.models import Entry, User

app = Flask(__name__)
app.config.from_object('bubble.config')

db = SQLAlchemy(app)

users = User.query.all()
print(users, User._get_password(users[0]))

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-01-18 (金) 07:57:02 (1531d)