Skip to content

Instantly share code, notes, and snippets.

@tylernchls
Last active March 29, 2018 20:09
Show Gist options
  • Save tylernchls/5b970b84a2aae1cd4ccfac9831301725 to your computer and use it in GitHub Desktop.
Save tylernchls/5b970b84a2aae1cd4ccfac9831301725 to your computer and use it in GitHub Desktop.
Instructions/overview for building onto flask sec application
  1. Build a virtual environment. This will manage dependancies on a per application basis ensuring not to conflict with globaly installed packages.

  2. Make an init.py file. This will hold your entire application existing as a package which is then imported and ran via server.py file by flask. This is where you will import packages and instantiate them assigning them to a variable ex. from flask_sqlalchemy import SQLAlchemy db=SQLAlchemy(app). Now can import db throughout the application.

  3. Make templates directory. This will hold all of your template files ending in .html for your application. You should have a base.hmtl file that will serve as your main .html file that all subsequent template files will be dynamically rendered via jinja2 through template inheritance. Other template files are rendered in base.html file where you place {%block content%}.

  4. Make forms.py file. This will hold all of the forms your application will need. They are represented via Classes ex Class LoginForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) password = PasswordField('Password', validators=[DataRequired()]) remember_me = BooleanField('Remember Me')            submit = SubmitField('Login') These forms can be imported into your routes.py file and will need to . instansiate a new instance of the class ex. form = Registration()

  5. Make routes.py file. This will hold all the routes for your application and extensive logic to handle different user input such as user login or registration. Each route will have a decorator making a relationship between the route and the specific view function that should be fired when a user hits that specific route of the application ex. @app.route('/index') def index:

  6. Add a new web page to the app. Once all of these files and directories are added then all you have to do to add a new page is:

    1. Figure out which modules you will need to provide functionality for the page.
    2. Import these modules into init.py and assign them to variables. Import where needed in your application.
    3. Create any models for your database that will be passed into view functions in either routes.py.
    4. Build any forms that will accept user input or display information from the database by creating a new class in forms.py
    5. Define the logic (view function) that will handle user data or http requests in routes.py
    6. Finally, write a new .html file in the templates directory which will display the information.
    7. Update base.html files navigation if needed.
    8. Handle form errors via jinja2 to display appropriate responses to the user for different situations ex. if user inputs wrong password, an error message should be displayed to the user and redirect user back to login page.
    9. Repeat as needed on a per page basis.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment