Skip to content

tuvali/Hakolboynik

Repository files navigation

Flask Skeleton Project

Flask skeleton project as a base project for app development


Description:

This is a base skeleton Flask project to start developing on.
You may want to modify some of the configurations and files as needed.

Keywords:

flask, lean, skeleton, project, structure, environment, setup, template, fullstack, web, development, app, university, education.

By Barak Pinchovski

LinkedIn

Setup and run instructions:

Prerequisites:

Open the project's dir in the terminal and run the following commands:

  1. pip install virtualenv
  2. virtualenv venv
  3. pip install python-dotenv

Configurations:

Open the project's dir in the terminal and:

  1. Run the command: python -c "import os; print(os.urandom(16))"
  2. Copy the output string
  3. Edit the project's .env file found inside the root folder
  4. Overwrite the SECRET_KEY value with the string you copied

Run:

Run the project with your IDE's configuration, or from the terminal by using the flask run command

Usage:

Connecting and querying a database:

  1. Edit .env file with your database details

METHOD 1 - Query the database by using the implemented DBManager module

  1. A module named DBManager exists for querying the database.

  2. On .py files you'd like to connect and query the database, use the following import:

    from utilities.db.db_manager import dbManager
  3. Then use one of the methods commit(query, args=()), fetch(query, args=()), execute(query, args=()) to query the database according to the explanation below:

    • commit(query, args=())
      Use this method for INSERT, UPDATE and DELETE queries.
      Returns number of affected rows.
    • fetch(query, args=())
      Use this method for SELECT queries.
      Returns:
      - The query result as a list of named tuples where fields are accessible with dot notation. Read more about it here.
      - False if query fails.
    • execute(query, args=())
      Use this method for CREATE..., ALTER... and DROP... queries.
      Returns:
      - True if query is successful.
      - False if query fails.

    Example:

    from utilities.db.db_manager import dbManager
    query_result = dbManager.fetch('SELECT * FROM example_table')
    if query_result:
        for record in query_result:
            print(record.id) #prints the record's ID 

METHOD 2 - DIY

  1. On .py files you'd like to connect to the database, use the following imports:

    from settings import DB
    import mysql.connector
  2. Connect to the database with **DB as the argument

    db_connection = mysql.connector.connect(**DB)
  3. Tip: If you'd like the queried results to behave like JavaScript objects then instantiate cursor as follows:

    db_cursor = db_connection.cursor(named_tuple=True)

    Once you do that, you'll be able access the properties with dot notation.

    Read more about it here

    Example:

    from settings import DB
    import mysql.connector
    db_connection = mysql.connector.connect(**DB)
    db_cursor = db_connection.cursor(named_tuple=True)
    db_cursor.execute('SELECT * FROM example_table')
    records = db_cursor.fetchall()
    for record in records:
        print(record.id) #prints the record's ID 

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published