Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #run this script and then pass the name of your project in as an argument
- #e.g. bash flask_scaffolding.sh new_flask_application
- #Making the application directory $1 is the name of the application
- mkdir $1
- cd $1
- #Create the virtual environment
- python3 -m venv ./env
- source ./env/bin/activate
- #Create the .gitignore
- echo "*.pyc" >> .gitignore
- echo "env/" >> .gitignore
- echo "__pycache__" >> .gitignore
- #Creating the setup.py file
- echo "from setuptools import setup" >> setup.py
- echo "setup(" >> setup.py
- echo " name='$1'," >> setup.py
- echo " packages=['$1']," >> setup.py
- echo " include_package_data=True," >> setup.py
- echo " install_requires=['flask',]," >> setup.py
- echo ")" >> setup.py
- #Creating the Manifest file
- echo "graft flaskr/templates" >> MANIFEST.in
- echo "graft flaskr/static" >> MANIFEST.in
- #Creating a new subdirectory which will actually contain the application code
- mkdir $1
- cd $1
- mkdir static templates
- echo "from .$1 import app" >> __init__.py
- #Creating the app file
- echo "import os" >> $1.py
- echo "from flask import (Flask, request, session, g, redirect, url_for, abort)" >> $1.py
- echo "from flask_script import Manager" >> $1.py
- echo "from flask_migrate import Migrate, MigrateCommand" >> $1.py
- echo "from $1.models import db, User" >> $1.py
- echo "app = Flask(__name__)" >> $1.py
- echo "#Configuring the mysql database" >> $1.py
- echo "DB_PASSWORD = 'replace this'" >> $1.py
- echo "DB_HOST = 'replace this'" >> $1.py
- echo "DB_NAME = 'replace this'" >> $1.py
- echo "DB_USER = 'replace this'" >> $1.py
- echo "SQLALCHEMY_DATABASE_URI = 'mysql://{}:{}@{}:3306/{}'.format(DB_USER, DB_PASSWORD, DB_HOST, DB_NAME)" >> $1.py
- echo "migrate = Migrate(app, db)" >> $1.py
- echo "manager = Manager(app)" >> $1.py
- echo "manager.add_command('db', MigrateCommand)" >> $1.py
- echo "import $1.view" >> $1.py
- echo "if __name__=='__main__':" >> $1.py
- echo " manager.run()" >> $1.py
- #Creating the models file
- echo "from flask_sqlalchemy import SQLAlchemy" >> models.py
- echo "from flask_login import UserMixin" >> models.py
- echo "db=SQLAlchemy()" >> models.py
- echo "class User(UserMixin, db.Model):" >> models.py
- echo " id = db.Column(db.Integer, primary_key=True, autoincrement=True)" >> models.py
- echo " username = db.Column(db.String(255), unique=True)" >> models.py
- echo " password = db.Column(db.String(255))" >> models.py
- echo " def __init__(self, username, password):" >> models.py
- echo " self.username = username" >> models.py
- echo " self.password = password" >> models.py
- #Creating the views file
- echo "from $1.$1 import app" >> views.py
- echo "from $1.models import db, User" >> views.py
- echo " " >> views.py
- echo "@app.route('/')" >> views.py
- echo "def index():" >> views.py
- echo " return('Hello World')" >> views.py
- echo " " >> views.py
- echo "@app.route('/add_user')" >> views.py
- echo "def add_user():" >> views.py
- echo " new_user = User(username='foo', password='bar')" >> views.py
- echo " db.session.add(new_user)" >> views.py
- echo " db.session.commit()" >> views.py
- echo " return 'Added user'" >> views.py
- #Now that everything should have been set up. Need to install everything.
- cd ..
- pip install -e .
- #Telling the user what to do next
- echo "All the files have been created. You will now need to do the following:"
- echo "run 'flask db init'"
- echo "run 'flask db migrate'"
- echo "run 'flask db upgrade'"
- echo "Once you have set up the database. You need to run this bad boy, which you do like this: "
- echo "export FLASK_APP=$1"
- echo "export FLASK_DEBUG=true"
- echo "flask run"
Add Comment
Please, Sign In to add comment