def main(argv): # validate args if len(argv) != 3: print("Provide a valid database path and port as argument!") exit(-1) try: db_path = argv[1] port = int(argv[2]) if ".." in db_path or port == 0: raise ValueError # config app app.config[ 'SQLALCHEMY_TRACK_MODIFICATIONS'] = True # allow overhead ;) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_path app.config['APP_PORT'] = port # init instead of creating -> allows us to have database models in a different file db.init_app(app) with app.app_context(): # we can only create all if we have the app context! ( also needed cause of separate file! ) db.create_all() app.run(debug=False, port=app.config['APP_PORT']) except ValueError: print("Provide a valid database path and port as argument!") exit(-1) except: print("Closing app") exit(-1)
def reloadDb(): print("Dropping all dables.") db.drop_all() db.session.commit() print("Creating tables for each model"); db.create_all() db.session.commit() print("Syncing questions") Question.loadQuestionsIntoDb(question_bank['questions'])
from flask import Flask from Models import db import os from Shop import Shop from Room import Room from Models import app, db, wasDB, api from Rooms import Rooms if not wasDB: print("A") db.create_all() api.add_resource(Shop,"/Shop/<int:shop_id>") api.add_resource(Room, "/Room/<int:room_id>") api.add_resource(Rooms,"/Rooms/") if __name__ == "__main__": app.run(debug=True)
session['userid'] = form.data.get('userid') #form에서 가져온 userid를 세션에 저장 return redirect('/') #성공하면 main.html로 return render_template('login.html', form=form) @app.route('/logout', methods=['GET']) def logout(): session.pop('userid', None) return redirect('/') if __name__ == "__main__": #데이터베이스--------- basedir = os.path.abspath(os.path.dirname(__file__)) #현재 파일이 있는 디렉토리 절대 경로 dbfile = os.path.join(basedir, 'db.sqlite') #데이터베이스 파일을 만든다 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + dbfile app.config[ 'SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True #사용자에게 정보 전달완료하면 teadown. 그 때마다 커밋=DB반영 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False #추가 메모리를 사용하므로 꺼둔다 app.config['SECRET_KEY'] = 'asdfasdfasdfqwerty' #해시값은 임의로 적음 csrf = CSRFProtect() csrf.init_app(app) db.init_app(app) #app설정값 초기화 db.app = app #Models.py에서 db를 가져와서 db.app에 app을 명시적으로 넣는다 db.create_all() #DB생성 app.run(host="127.0.0.1", port=5000, debug=True)
mimetype='application/json') @app.route('/info') def about(): try: f = open("warreport.txt", 'r') report = [] for line in f: report.append(line) print(report) return render_template( 'Warreport.html', user=Player.query.filter_by(username=session['user']).first(), text=report) except: return redirect(url_for('login')) @app.before_request def before_request(): g.user = None if 'user' in session: g.user = session['user'] if __name__ == "__main__": db.init_app(app=app) db.create_all(app=app) app.run(debug=True)
UPLOAD_FOLDER = '/home/shubham/Desktop/web_development/tutplus/data/user_dp/' ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg']) app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.secret_key = "A0Zr98j/3yX R~XHH!jmN]LWX/,?RT" app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://*****:*****@10.5.18.68/13CS30030" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db.init_app(app) login_manager = LoginManager() login_manager.session_protection = "strong" # login_serializer = URLSafeTimedSerializer(app.secret_key) login_manager.init_app(app) with app.app_context(): db.create_all() @app.before_request def before_request(): g.user = current_user @login_manager.user_loader def user_loader(user_id): """Given *user_id*, return the associated User object. :param unicode user_id: user_id (email) user to retrieve """ return User.query.get(int(user_id))