__init__.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from flask import Flask, render_template, url_for
  2. from flask_sqlalchemy import SQLAlchemy
  3. from flask_bcrypt import Bcrypt
  4. 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
  5. from project.forms import PlayerPointsForm
  6. from project.routes import api, auth, singleplayer, limiter
  7. from PIL import Image as PImage
  8. import os
  9. from project.routes import login_manager
  10. from flask_login import login_required
  11. from base64 import b64encode
  12. from io import BytesIO
  13. def create_app(testing=False):
  14. app = Flask(__name__)
  15. app.config.from_object("project.config.Config")
  16. db.init_app(app)
  17. migrate.init_app(app, db)
  18. login_manager.init_app(app)
  19. login_manager.login_view = 'auth.login' # so that all endpoints with @login_required are redirected to login if not authenticated
  20. limiter.init_app(app)
  21. app.register_blueprint(api, url_prefix="/api")
  22. app.register_blueprint(singleplayer, url_prefix="/singleplayer")
  23. app.register_blueprint(auth)
  24. @app.route("/", methods=['GET'])
  25. @app.route("/index", methods=['GET'])
  26. @login_required
  27. def index():
  28. # form = PlayerPointsForm()
  29. data = get_all_games()
  30. game_ids = []
  31. for row in data:
  32. if row.Game.id not in game_ids:
  33. game_ids.append(row.Game.id)
  34. imgs = get_all_images()
  35. images = {}
  36. for img in imgs:
  37. if f"{img.game_id}" not in images:
  38. images[f"{img.game_id}"] = []
  39. image = PImage.open(img.img_path)
  40. image.thumbnail((30, 30)) #compress the image for viewing in table
  41. image_io = BytesIO()
  42. image.save(image_io, 'PNG')
  43. images[f"{img.game_id}"].append('data:image/png;base64,' + b64encode(image_io.getvalue()).decode('ascii'))
  44. action_cards = get_all_action_cards()
  45. char_cards = get_all_char_cards()
  46. goal_cards = get_all_goal_cards()
  47. return render_template('index.html', title='SmartHubs', data=data, images=images, games=game_ids,
  48. action_cards=action_cards, char_cards=char_cards, goal_cards=goal_cards)
  49. return app
  50. if __name__ == '__main__':
  51. app = create_app()
  52. app.run(debug=True)
  53. # sched = BackgroundScheduler(daemon=True)
  54. # sched.add_job(routes.algorithm,'interval',seconds = 60)
  55. # sched.init_app(app)
  56. # sched.start()
  57. # atexit.register(lambda: sched.shutdown())