from flask import Flask, render_template, url_for from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt from project.models import db, Game, Player, Object, Image, User, get_all_games, get_all_images, migrate, get_all_action_cards, get_all_char_cards, get_all_goal_cards from project.forms import PlayerPointsForm from project.routes import api, auth, singleplayer, limiter from PIL import Image as PImage import os from project.routes import login_manager from flask_login import login_required from base64 import b64encode from io import BytesIO def create_app(testing=False): app = Flask(__name__) app.config.from_object("project.config.Config") db.init_app(app) migrate.init_app(app, db) login_manager.init_app(app) login_manager.login_view = 'auth.login' # so that all endpoints with @login_required are redirected to login if not authenticated limiter.init_app(app) app.register_blueprint(api, url_prefix="/api") app.register_blueprint(singleplayer, url_prefix="/singleplayer") app.register_blueprint(auth) @app.route("/", methods=['GET']) @app.route("/index", methods=['GET']) @login_required def index(): # form = PlayerPointsForm() data = get_all_games() game_ids = [] for row in data: if row.Game.id not in game_ids: game_ids.append(row.Game.id) imgs = get_all_images() images = {} for img in imgs: if f"{img.game_id}" not in images: images[f"{img.game_id}"] = [] image = PImage.open(img.img_path) image.thumbnail((30, 30)) #compress the image for viewing in table image_io = BytesIO() image.save(image_io, 'PNG') images[f"{img.game_id}"].append('data:image/png;base64,' + b64encode(image_io.getvalue()).decode('ascii')) action_cards = get_all_action_cards() char_cards = get_all_char_cards() goal_cards = get_all_goal_cards() return render_template('index.html', title='SmartHubs', data=data, images=images, games=game_ids, action_cards=action_cards, char_cards=char_cards, goal_cards=goal_cards) return app if __name__ == '__main__': app = create_app() app.run(debug=True) # sched = BackgroundScheduler(daemon=True) # sched.add_job(routes.algorithm,'interval',seconds = 60) # sched.init_app(app) # sched.start() # atexit.register(lambda: sched.shutdown())