├── project ├── adm │ ├── __init__.py │ └── routes.py ├── main │ ├── __init__.py │ └── routes.py ├── site.db ├── templates │ ├── home.html │ └── layout.html ├── config.py ├── __init__.py └── models.py ├── .gitignore ├── run.py ├── requirements.txt └── README.md /project/adm/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /project/main/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .myvenv 2 | myvenv/ 3 | __pycache__/ 4 | -------------------------------------------------------------------------------- /project/site.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/the-akira/flask-init/master/project/site.db -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | from project import create_app 2 | 3 | app = create_app() 4 | 5 | if __name__ == '__main__': 6 | app.run(debug=True) -------------------------------------------------------------------------------- /project/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | 3 | {% block content %} 4 |

Flask Boilerplate

5 | {% endblock content %} -------------------------------------------------------------------------------- /project/config.py: -------------------------------------------------------------------------------- 1 | class Config: 2 | SECRET_KEY = 'ab57ccec0f56942a5ca33215f9d2d88c' 3 | SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db' 4 | SQLALCHEMY_TRACK_MODIFICATIONS = False -------------------------------------------------------------------------------- /project/main/routes.py: -------------------------------------------------------------------------------- 1 | from flask import render_template, request, Blueprint 2 | 3 | main = Blueprint('main', __name__) 4 | 5 | @main.route("/") 6 | @main.route("/home") 7 | def home(): 8 | return render_template('home.html') -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Click==7.0 2 | Flask==1.1.1 3 | Flask-Admin==1.5.5 4 | Flask-SQLAlchemy==2.4.1 5 | itsdangerous==1.1.0 6 | Jinja2==2.11.3 7 | MarkupSafe==1.1.1 8 | SQLAlchemy==1.3.13 9 | Werkzeug==1.0.0 10 | WTForms==2.2.1 11 | -------------------------------------------------------------------------------- /project/adm/routes.py: -------------------------------------------------------------------------------- 1 | from flask import Blueprint 2 | from project.models import User, Game, Genres 3 | from project import admin, db 4 | from flask_admin.contrib.sqla import ModelView 5 | 6 | adm = Blueprint('adm', __name__) 7 | 8 | admin.add_view(ModelView(User, db.session)) 9 | admin.add_view(ModelView(Game, db.session)) 10 | admin.add_view(ModelView(Genres, db.session)) -------------------------------------------------------------------------------- /project/templates/layout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | {% block content %}{% endblock %} 14 | 15 | -------------------------------------------------------------------------------- /project/__init__.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_admin import Admin 3 | from flask_sqlalchemy import SQLAlchemy 4 | from project.config import Config 5 | 6 | db = SQLAlchemy() 7 | admin = Admin() 8 | 9 | def create_app(config_class=Config): 10 | app = Flask(__name__) 11 | app.config.from_object(Config) 12 | 13 | db.init_app(app) 14 | admin.init_app(app) 15 | 16 | from project.main.routes import main 17 | from project.adm.routes import adm 18 | app.register_blueprint(main) 19 | app.register_blueprint(adm) 20 | 21 | return app -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flask Init 2 | 3 | A Boilerplate code for small data-driven Flask applications 4 | 5 | ## Installation 6 | 7 | ### Clone the Repository 8 | 9 | ``` 10 | git clone https://github.com/the-akira/flask-init.git 11 | ``` 12 | 13 | ### Inside the Main Directory 14 | 15 | Create a Virtual Environment 16 | 17 | ``` 18 | python -m venv myvenv 19 | ``` 20 | 21 | Activate the Virtual Environment 22 | 23 | ``` 24 | source myvenv/bin/activate 25 | ``` 26 | 27 | Install Requirements 28 | 29 | ``` 30 | pip install -r requirements.txt 31 | ``` 32 | 33 | Run the Application 34 | 35 | ``` 36 | python run.py 37 | ``` 38 | 39 | - Navigate to `http://127.0.0.1:5000/` to see the Homepage 40 | - Navigate to `http://127.0.0.1:5000/admin` to see the Administrative Interface 41 | -------------------------------------------------------------------------------- /project/models.py: -------------------------------------------------------------------------------- 1 | from project import db 2 | from datetime import datetime 3 | 4 | """ 5 | from project import create_app 6 | from project import db 7 | app = create_app() 8 | app.app_context().push() 9 | db.create_all() 10 | """ 11 | 12 | class User(db.Model): 13 | id = db.Column(db.Integer, primary_key=True) 14 | username = db.Column(db.String(20), unique=True, nullable=False) 15 | email = db.Column(db.String(120), unique=True, nullable=False) 16 | password = db.Column(db.String(60), nullable=False) 17 | 18 | def __repr__(self): 19 | return f"User('{self.username}', '{self.email}')" 20 | 21 | class Game(db.Model): 22 | id = db.Column(db.Integer, primary_key=True) 23 | name = db.Column(db.String(50), nullable=False) 24 | publisher = db.Column(db.String(50), nullable=False) 25 | director = db.Column(db.String(50), nullable=False) 26 | genres = db.relationship('Genres', backref='game', lazy=True) 27 | 28 | def __repr__(self): 29 | return f"Game('{self.id}: {self.name}', '{self.publisher}')" 30 | 31 | class Genres(db.Model): 32 | id = db.Column(db.Integer, primary_key=True) 33 | name = db.Column(db.String(120), nullable=False) 34 | game_id = db.Column(db.Integer, db.ForeignKey('game.id'),nullable=False) --------------------------------------------------------------------------------