app.runが機能しない
解決したいこと
こちらのページを参考に、webアプリを開発しています。app.runが機能しません。flask_auth_appディレクトリに移動してコマンド
$ flask run
を使えば実行はできるのですが、並列化したくないので
app.run(use_reloader=False, threaded=False)
にしようと思っています。
階層構造
.
└── flask_auth_app
└── project
├── __init__.py
├── auth.py
├── SA.py
├── db.sqlite
├── main.py
├── models.py
└── templates
├── base.html
├── index.html
├── login.html
├── profile.html
└── signup.html
└── instance
└── db.sqlite
__init.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret-key-goes-here'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db.init_app(app)
from . import models
with app.app_context():
db.create_all()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from .models import User
@login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
# blueprint for auth routes in our app
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
app.run(use_reloader=False, threaded=False)
return app
main.py
from flask import Blueprint, render_template, request
from . import db
from flask_login import login_required, current_user
import numpy as np
import sqlite3
from . import SA
app = create_app()
main = Blueprint('main', __name__)
@main.route('/')
def index():
return render_template('index.html')
@main.route('/profile')
def profile():
img = SA.make_img(current_user.id)
#SA.pyを用いてプロフィール情報を加工し、表示する部分
#SAを用いているため並列化が不可能
return render_template('profile.html', name=current_user.name, image = img)
if __name__ == "__main__":
app.run(debug=True, use_reloader=False, threaded=False)
SA.py
from simanneal import Annealer
def make_img(user_id):
#ユーザーごとに加工した画像を作成する部分
return img
models.py
from flask_login import UserMixin
from . import db
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
name = db.Column(db.String(1000))
interest = db.Column(db.String(1)) #自らの美的センスに対するアンケート情報を格納する
auth.py
from flask import Blueprint, render_template, redirect, url_for, request, flash
from . import db
from werkzeug.security import generate_password_hash, check_password_hash
from .models import User
from flask_login import login_user, logout_user, login_required
from .models import User
auth = Blueprint('auth', __name__)
@auth.route('/login')
def login():
return render_template('login.html')
@auth.route('/login', methods=['POST'])
def login_post():
email = request.form.get('email')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
# emailは存在してるよね?
user = User.query.filter_by(email=email).first()
# check if the user actually exists
# take the user-supplied password, hash it, and compare it to the hashed password in the database
if not user or not check_password_hash(user.password, password):
flash('Please check your login details and try again.')
return redirect(url_for('auth.login')) # if the user doesn't exist or password is wrong, reload the page
# if the above check passes, then we know the user has the right credentials
login_user(user, remember=remember)
return redirect(url_for('main.profile'))
@auth.route('/signup')
def signup():
return render_template('signup.html')
@auth.route('/signup', methods=['POST'])
def signup_post():
# formの入力内容を取得
email = request.form.get('email')
name = request.form.get('name')
password = request.form.get('password')
interest = request.form.get('interest')
user = User.query.filter_by(email=email).first()
# if this returns a user, then the email already exists in database
if user:
# if a user is found, we want to redirect back to signup page so user can try again
flash('Email address already exists')
return redirect(url_for('auth.signup'))
# create a new user with the form data. Hash the password so the plaintext version isn't saved.
new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'), interest=interest)
# add the new user to the database
db.session.add(new_user)
db.session.commit()
return redirect(url_for('auth.login'))
@auth.route('/logout')
def logout():
logout_user()
return redirect(url_for('main.index'))
htmlファイルはprofile.html以外は参考にしたところと同じです。profile.htmlも画像を表示する以外は全て同様です。
自分で試したこと
$ flask run
を実行すると、simannealパッケージを使用しているため
ValueError: signal only works in main thread of the main interpreter
が出ます。
皆様のお力添えをいただければ幸いです。
0