Guest User

Untitled

a guest
Apr 9th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #run this script and then pass the name of your project in as an argument
  2. #e.g. bash flask_scaffolding.sh new_flask_application
  3. #Making the application directory $1 is the name of the application
  4. mkdir $1
  5. cd $1
  6.  
  7. #Create the virtual environment
  8. python3 -m venv ./env
  9. source ./env/bin/activate
  10.  
  11. #Create the .gitignore
  12. echo "*.pyc" >> .gitignore
  13. echo "env/" >> .gitignore
  14. echo "__pycache__" >> .gitignore
  15.  
  16. #Creating the setup.py file
  17. echo "from setuptools import setup" >> setup.py
  18. echo "setup(" >> setup.py
  19. echo " name='$1'," >> setup.py
  20. echo " packages=['$1']," >> setup.py
  21. echo " include_package_data=True," >> setup.py
  22. echo " install_requires=['flask',]," >> setup.py
  23. echo ")" >> setup.py
  24.  
  25. #Creating the Manifest file
  26. echo "graft flaskr/templates" >> MANIFEST.in
  27. echo "graft flaskr/static" >> MANIFEST.in
  28.  
  29. #Creating a new subdirectory which will actually contain the application code
  30. mkdir $1
  31. cd $1
  32.  
  33. mkdir static templates
  34.  
  35.  
  36. echo "from .$1 import app" >> __init__.py
  37.  
  38. #Creating the app file
  39. echo "import os" >> $1.py
  40. echo "from flask import (Flask, request, session, g, redirect, url_for, abort)" >> $1.py
  41. echo "from flask_script import Manager" >> $1.py
  42. echo "from flask_migrate import Migrate, MigrateCommand" >> $1.py
  43. echo "from $1.models import db, User" >> $1.py
  44.  
  45. echo "app = Flask(__name__)" >> $1.py
  46. echo "#Configuring the mysql database" >> $1.py
  47.  
  48. echo "DB_PASSWORD = 'replace this'" >> $1.py
  49. echo "DB_HOST = 'replace this'" >> $1.py
  50. echo "DB_NAME = 'replace this'" >> $1.py
  51. echo "DB_USER = 'replace this'" >> $1.py
  52.  
  53. echo "SQLALCHEMY_DATABASE_URI = 'mysql://{}:{}@{}:3306/{}'.format(DB_USER, DB_PASSWORD, DB_HOST, DB_NAME)" >> $1.py
  54.  
  55. echo "migrate = Migrate(app, db)" >> $1.py
  56. echo "manager = Manager(app)" >> $1.py
  57. echo "manager.add_command('db', MigrateCommand)" >> $1.py
  58.  
  59. echo "import $1.view" >> $1.py
  60.  
  61. echo "if __name__=='__main__':" >> $1.py
  62. echo " manager.run()" >> $1.py
  63.  
  64. #Creating the models file
  65. echo "from flask_sqlalchemy import SQLAlchemy" >> models.py
  66. echo "from flask_login import UserMixin" >> models.py
  67. echo "db=SQLAlchemy()" >> models.py
  68.  
  69. echo "class User(UserMixin, db.Model):" >> models.py
  70. echo " id = db.Column(db.Integer, primary_key=True, autoincrement=True)" >> models.py
  71. echo " username = db.Column(db.String(255), unique=True)" >> models.py
  72. echo " password = db.Column(db.String(255))" >> models.py
  73.  
  74. echo " def __init__(self, username, password):" >> models.py
  75. echo " self.username = username" >> models.py
  76. echo " self.password = password" >> models.py
  77.  
  78. #Creating the views file
  79. echo "from $1.$1 import app" >> views.py
  80. echo "from $1.models import db, User" >> views.py
  81.  
  82. echo " " >> views.py
  83. echo "@app.route('/')" >> views.py
  84. echo "def index():" >> views.py
  85. echo " return('Hello World')" >> views.py
  86.  
  87. echo " " >> views.py
  88. echo "@app.route('/add_user')" >> views.py
  89. echo "def add_user():" >> views.py
  90. echo " new_user = User(username='foo', password='bar')" >> views.py
  91. echo " db.session.add(new_user)" >> views.py
  92. echo " db.session.commit()" >> views.py
  93. echo " return 'Added user'" >> views.py
  94.  
  95. #Now that everything should have been set up. Need to install everything.
  96. cd ..
  97. pip install -e .
  98.  
  99. #Telling the user what to do next
  100. echo "All the files have been created. You will now need to do the following:"
  101. echo "run 'flask db init'"
  102. echo "run 'flask db migrate'"
  103. echo "run 'flask db upgrade'"
  104.  
  105. echo "Once you have set up the database. You need to run this bad boy, which you do like this: "
  106. echo "export FLASK_APP=$1"
  107. echo "export FLASK_DEBUG=true"
  108. echo "flask run"
Add Comment
Please, Sign In to add comment