├── .gitignore
├── README.md
├── Session 1 - Introduction to Flask
├── Session 1 - notes.pdf
├── __pycache__
│ └── mod2.cpython-312.pyc
├── app.py
├── mod1.py
└── mod2.py
├── Session 2 - Dynamic URL, Redirection, URL Building
├── Session 2 - notes.pdf
├── dynamic_urls.py
├── url_building.py
└── url_redirection.py
├── Session 3 - Jinja, Templates & Inheritance
├── Session 3 - notes.pdf
├── app.py
├── employees.py
└── templates
│ ├── about.html
│ ├── employees.html
│ ├── evaluate.html
│ ├── home.html
│ ├── layout.html
│ └── managers.html
├── Session 4 - Web Forms & Input Validation
├── Session 4 - notes.pdf
├── app.py
├── forms.py
└── templates
│ ├── home.html
│ ├── layout.html
│ ├── login.html
│ └── signup.html
├── Session 5 - Databases
├── Session 5 - notes.pdf
├── db_crud_demo.py
├── many_to_many.py
└── one_to_many.py
├── Session 6 - Sessions
├── Session 6 - notes.pdf
├── client_sessions.py
├── flask_session
│ ├── 2029240f6d1128be89ddc32729463129
│ └── 677e546b8071aa256de8ee836ffb22f5
├── forms.py
├── server_sessions.py
└── templates
│ ├── about.html
│ ├── contact.html
│ ├── home.html
│ ├── layout.html
│ └── login.html
├── Session 7 - Cookies
├── Session 7 - notes.pdf
├── demo_1.py
├── demo_2.py
├── forms.py
└── templates
│ ├── about.html
│ ├── contact.html
│ ├── home.html
│ ├── layout.html
│ └── login.html
└── Session 8 - Training & Deloyment of ML Model
├── README.md
└── Session 8 - notes.pdf
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | share/python-wheels/
24 | *.egg-info/
25 | .installed.cfg
26 | *.egg
27 | MANIFEST
28 |
29 | # PyInstaller
30 | # Usually these files are written by a python script from a template
31 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
32 | *.manifest
33 | *.spec
34 |
35 | # Installer logs
36 | pip-log.txt
37 | pip-delete-this-directory.txt
38 |
39 | # Unit test / coverage reports
40 | htmlcov/
41 | .tox/
42 | .nox/
43 | .coverage
44 | .coverage.*
45 | .cache
46 | nosetests.xml
47 | coverage.xml
48 | *.cover
49 | *.py,cover
50 | .hypothesis/
51 | .pytest_cache/
52 | cover/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | .pybuilder/
76 | target/
77 |
78 | # Jupyter Notebook
79 | .ipynb_checkpoints
80 |
81 | # IPython
82 | profile_default/
83 | ipython_config.py
84 |
85 | # pyenv
86 | # For a library or package, you might want to ignore these files since the code is
87 | # intended to run in multiple environments; otherwise, check them in:
88 | # .python-version
89 |
90 | # pipenv
91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
94 | # install all needed dependencies.
95 | #Pipfile.lock
96 |
97 | # poetry
98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99 | # This is especially recommended for binary packages to ensure reproducibility, and is more
100 | # commonly ignored for libraries.
101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102 | #poetry.lock
103 |
104 | # pdm
105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106 | #pdm.lock
107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108 | # in version control.
109 | # https://pdm.fming.dev/#use-with-ide
110 | .pdm.toml
111 |
112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113 | __pypackages__/
114 |
115 | # Celery stuff
116 | celerybeat-schedule
117 | celerybeat.pid
118 |
119 | # SageMath parsed files
120 | *.sage.py
121 |
122 | # Environments
123 | .env
124 | .venv
125 | env/
126 | venv/
127 | ENV/
128 | env.bak/
129 | venv.bak/
130 |
131 | # Spyder project settings
132 | .spyderproject
133 | .spyproject
134 |
135 | # Rope project settings
136 | .ropeproject
137 |
138 | # mkdocs documentation
139 | /site
140 |
141 | # mypy
142 | .mypy_cache/
143 | .dmypy.json
144 | dmypy.json
145 |
146 | # Pyre type checker
147 | .pyre/
148 |
149 | # pytype static type analyzer
150 | .pytype/
151 |
152 | # Cython debug symbols
153 | cython_debug/
154 |
155 | # PyCharm
156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158 | # and can be added to the global gitignore or merged into this file. For a more nuclear
159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160 | #.idea/
161 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Flask Course
2 |
3 | ## Overview
4 |
5 | Welcome to the Flask Course repository! This course is designed to provide an in-depth understanding of Flask, a popular web framework for Python 🐍 Across 8 comprehensive sessions, you will learn the fundamental concepts of web development 💻 with Flask, ranging from basic URL building to deploying machine learning models. Each session includes relevant theory and practical web application examples 🚀 with code.
6 |
7 | ## Course Contents
8 |
9 | ### Session 1: Introduction to Flask
10 | - Overview of Flask and its features
11 | - Understanding how web applications work
12 | - Basic web concepts: HTTP, request-response cycle, web servers
13 | - Your first Flask application
14 |
15 | ### Session 2: Dynamic URLs, URL RedirectionURL and URL Building
16 | - Understanding URL routes in Flask
17 | - Building dynamic URLs with Flask
18 | - Implementing URL redirection
19 | - Examples of URL routing and redirection in a web application
20 |
21 | ### Session 3: HTML Templates, Jinja Templates, and Template Inheritance
22 | - Introduction to HTML templates in Flask
23 | - Using Jinja2 templating engine
24 | - Template inheritance and reusability
25 | - Creating dynamic web pages with Jinja2
26 |
27 | ### Session 4: Web Forms and Input Validation using WTForms
28 | - Handling web forms in Flask
29 | - Introduction to WTForms
30 | - Creating and validating web forms
31 | - Practical example of form handling and data validation using Flask
32 |
33 | ### Session 5: Databases with Flask
34 | - Understanding key database concepts
35 | - Understanding the roles and working of an ORM
36 | - Setting up SQLAlchemy with Flask
37 | - Working with SQLite database and SQLite Studio
38 | - Performing CRUD operations and establish table relationships using SQLAlchemy and SQLite database
39 |
40 | ### Session 6: Sessions
41 | - Understanding the definition and working of sessions in web applications
42 | - Understanding the various categorizations of sessions
43 | - Understanding the security considerations behind sessions
44 | - Examples of client and server session handling in Flask applications
45 |
46 | ### Session 7: Cookies
47 | - Introduction to web cookies and their working
48 | - Understanding the security considerations behind cookies
49 | - Setting and retrieving cookies in Flask
50 |
51 | ### Session 8: Training and Deployment of Machine Learning Models
52 | - Performing model seelction using learning curves
53 | - Training a machine learning model using Scikit-learn
54 | - Serving a machine learning model as an API using Flask
55 | - Deploying Flask applications using Render
56 |
57 |
58 | Happy learning! 🌟
59 | Hope you enjoy the course 😊
60 |
--------------------------------------------------------------------------------
/Session 1 - Introduction to Flask/Session 1 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 1 - Introduction to Flask/Session 1 - notes.pdf
--------------------------------------------------------------------------------
/Session 1 - Introduction to Flask/__pycache__/mod2.cpython-312.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 1 - Introduction to Flask/__pycache__/mod2.cpython-312.pyc
--------------------------------------------------------------------------------
/Session 1 - Introduction to Flask/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 |
3 | # create the flask app
4 | app = Flask(__name__)
5 |
6 | # home page
7 | @app.route("/")
8 | @app.route("/home")
9 | def home():
10 | return "
Welcome to the Home Page!
"
11 |
12 |
13 | # about page
14 | @app.route("/about")
15 | def about():
16 | return "Welcome to the About Page!
"
17 |
18 |
19 | # example of path parameter
20 | @app.route("/welcome/")
21 | def welcome(name):
22 | return f"Hi {name.title()}, you're welcome to this Page!
"
23 |
24 |
25 | # example of integer path parameter
26 | @app.route("/addition/")
27 | def addition(num):
28 | return f"Input is {num}, Output is {num + 10}
"
29 |
30 |
31 | # example of two integer path parameters
32 | @app.route("/addition_two//")
33 | def addition_two(num1, num2):
34 | return f"{num1} + {num2} is {num1 + num2}
"
35 |
36 |
37 | # start the app
38 | if __name__ == "__main__":
39 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 1 - Introduction to Flask/mod1.py:
--------------------------------------------------------------------------------
1 | import mod2
2 |
3 | print(f"Running mod1 - ({__name__})")
--------------------------------------------------------------------------------
/Session 1 - Introduction to Flask/mod2.py:
--------------------------------------------------------------------------------
1 | if __name__ == "__main__":
2 | print(f"Running mod2 - ({__name__})")
--------------------------------------------------------------------------------
/Session 2 - Dynamic URL, Redirection, URL Building/Session 2 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 2 - Dynamic URL, Redirection, URL Building/Session 2 - notes.pdf
--------------------------------------------------------------------------------
/Session 2 - Dynamic URL, Redirection, URL Building/dynamic_urls.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 |
3 | app = Flask(__name__)
4 |
5 | @app.route("/")
6 | def home():
7 | return "Welcome to the Home page!
"
8 |
9 |
10 | @app.route("/welcome/")
11 | def welcome(name):
12 | return f"Hey {name.title()}, welcome to our Webpage!
"
13 |
14 |
15 | if __name__ == "__main__":
16 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 2 - Dynamic URL, Redirection, URL Building/url_building.py:
--------------------------------------------------------------------------------
1 | import time
2 | from flask import Flask, redirect, url_for
3 |
4 | app = Flask(__name__)
5 |
6 | @app.route("/")
7 | def home():
8 | return "Welcome to the Home page!
"
9 |
10 |
11 | @app.route("/pass//")
12 | def passed(sname, marks):
13 | return f"Congratz {sname.title()}, you've passed with {marks} marks!
"
14 |
15 |
16 | @app.route("/fail//")
17 | def failed(sname, marks):
18 | return f"Sorry {sname.title()}, you've failed with {marks} marks!
"
19 |
20 |
21 | @app.route("/score//")
22 | def score(name, num):
23 | if num < 30:
24 | time.sleep(1)
25 | # redirect user to page 'fail'
26 | return redirect(url_for("failed", sname=name, marks=num))
27 | else:
28 | time.sleep(1)
29 | # redirect user to page 'pass'
30 | return redirect(url_for("passed", sname=name, marks=num))
31 |
32 |
33 | if __name__ == "__main__":
34 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 2 - Dynamic URL, Redirection, URL Building/url_redirection.py:
--------------------------------------------------------------------------------
1 | import time
2 | from flask import Flask, redirect, url_for
3 |
4 | app = Flask(__name__)
5 |
6 | @app.route("/")
7 | def home():
8 | return "Welcome to the Home page!
"
9 |
10 |
11 | @app.route("/pass")
12 | def passed():
13 | return "Congratz, you've passed!
"
14 |
15 |
16 | @app.route("/fail")
17 | def failed():
18 | return "Sorry, you've failed!
"
19 |
20 |
21 | @app.route("/score//")
22 | def score(name, num):
23 | if num < 30:
24 | time.sleep(1)
25 | # redirect user to page 'fail'
26 | return redirect(url_for("failed"))
27 | else:
28 | time.sleep(1)
29 | # redirect user to page 'pass'
30 | return redirect(url_for("passed"))
31 |
32 |
33 | if __name__ == "__main__":
34 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/Session 3 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 3 - Jinja, Templates & Inheritance/Session 3 - notes.pdf
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, render_template, url_for
2 | from employees import employees_data
3 |
4 | # create the flask app
5 | app = Flask(__name__)
6 |
7 | # home page
8 | @app.route("/")
9 | @app.route("/home")
10 | def home():
11 | return render_template("home.html", title="Home")
12 |
13 |
14 | # about page
15 | @app.route("/about")
16 | def about():
17 | return render_template("about.html", title="About")
18 |
19 |
20 | # employees page
21 | @app.route("/employees")
22 | def employees():
23 | return render_template(
24 | "employees.html",
25 | title="Employees",
26 | emps=employees_data
27 | )
28 |
29 |
30 | # managers page
31 | @app.route("/employees/managers")
32 | def managers():
33 | return render_template(
34 | "managers.html",
35 | title="Managers",
36 | emps=employees_data
37 | )
38 |
39 |
40 | # demonstrating if-else with jinja
41 | @app.route("/evaluate/")
42 | def evaluate(num):
43 | return render_template(
44 | "evaluate.html",
45 | title="Evaluate",
46 | number=num
47 | )
48 |
49 |
50 | # start the app
51 | if __name__ == "__main__":
52 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/employees.py:
--------------------------------------------------------------------------------
1 | # dummy database
2 | employees_data = {
3 | 1: {
4 | "name": "Michael",
5 | "age": 42,
6 | "position": "Manager"
7 | },
8 | 2: {
9 | "name": "Dwight",
10 | "age": 35,
11 | "position": "Salesperson"
12 | },
13 | 3: {
14 | "name": "Pam",
15 | "age": 26,
16 | "position": "Salesperson"
17 | },
18 | 4: {
19 | "name": "Jim",
20 | "age": 31,
21 | "position": "Salesperson"
22 | },
23 | 5: {
24 | "name": "Josh",
25 | "age": 40,
26 | "position": "Manager"
27 | }
28 | }
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/templates/about.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 |
5 |
This is the About page of abc.
6 |
We are a pioneer in the field of AI and ML.
7 |
8 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/templates/employees.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 | {% for emp_id, emp_data in emps.items() %}
5 |
6 |
7 |
8 | ID: |
9 | {{ emp_id }} |
10 |
11 |
12 | Name: |
13 | {{ emp_data["name"] }} |
14 |
15 |
16 | Age: |
17 | {{ emp_data["age"] }} |
18 |
19 |
20 | Position: |
21 | {{ emp_data["position"] }} |
22 |
23 |
24 |
25 |
26 | {% endfor %}
27 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/templates/evaluate.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 | Given input is {{ number }}
5 | {% if number == 0 %}
6 | The given input {{number }} is neither Even nor Odd!
7 | {% elif number % 2 == 0 %}
8 | The given input {{number }} is Even!
9 | {% else %}
10 | The given input {{number }} is Odd!
11 | {% endif %}
12 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/templates/home.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 | Contents:
5 |
6 | - Home
7 | - About
8 | - Employees
9 |
12 | - Careers
13 | - Contact Us
14 |
15 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/templates/layout.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | {{ title }}
7 |
8 |
9 | Welcome to the {{ title }} page!
10 | {% block content %}
11 | {% endblock %}
12 |
13 |
--------------------------------------------------------------------------------
/Session 3 - Jinja, Templates & Inheritance/templates/managers.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 | {% for emp_id, emp_data in emps.items() %}
5 | {% if emp_data["position"].lower() == "manager" %}
6 |
7 |
8 |
9 | ID: |
10 | {{ emp_id }} |
11 |
12 |
13 | Name: |
14 | {{ emp_data["name"] }} |
15 |
16 |
17 | Age: |
18 | {{ emp_data["age"] }} |
19 |
20 |
21 | Position: |
22 | {{ emp_data["position"] }} |
23 |
24 |
25 |
26 |
27 | {% endif %}
28 | {% endfor %}
29 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 4 - Web Forms & Input Validation/Session 4 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 4 - Web Forms & Input Validation/Session 4 - notes.pdf
--------------------------------------------------------------------------------
/Session 4 - Web Forms & Input Validation/app.py:
--------------------------------------------------------------------------------
1 | from flask import (
2 | Flask,
3 | render_template,
4 | redirect,
5 | url_for,
6 | flash
7 | )
8 | from forms import SignupForm, LoginForm
9 |
10 | app = Flask(__name__)
11 | app.config["SECRET_KEY"] = "this_is_a_secret_key"
12 |
13 | @app.route("/")
14 | @app.route("/home")
15 | def home():
16 | return render_template("home.html", title="Home")
17 |
18 |
19 | @app.route("/signup", methods=["GET", "POST"])
20 | def signup():
21 | form = SignupForm()
22 | if form.validate_on_submit():
23 | flash(f"Successfully Registered {form.username.data}!")
24 | return redirect(url_for("home"))
25 | return render_template("signup.html", title="Sign Up", form=form)
26 |
27 |
28 | @app.route("/login", methods=["GET", "POST"])
29 | def login():
30 | form = LoginForm()
31 | email = form.email.data
32 | pw = form.password.data
33 | if form.validate_on_submit():
34 | if email == "a@b.com" and pw == "12345":
35 | flash("Logged in Successfully!")
36 | return redirect(url_for("home"))
37 | else:
38 | flash("Incorrect email or password")
39 | return render_template("login.html", title="Login", form=form)
40 |
41 |
42 | if __name__ == "__main__":
43 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 4 - Web Forms & Input Validation/forms.py:
--------------------------------------------------------------------------------
1 | from flask_wtf import FlaskForm
2 | from wtforms import (
3 | StringField,
4 | SelectField,
5 | DateField,
6 | PasswordField,
7 | SubmitField,
8 | BooleanField
9 | )
10 | from wtforms.validators import (
11 | DataRequired,
12 | Length,
13 | Email,
14 | Optional,
15 | EqualTo
16 | )
17 |
18 |
19 | class SignupForm(FlaskForm):
20 | username = StringField(
21 | "Username",
22 | validators=[DataRequired(), Length(2, 30)]
23 | )
24 | email = StringField(
25 | "Email",
26 | validators=[DataRequired(), Email()]
27 | )
28 | gender = SelectField(
29 | "Gender",
30 | choices=["Male", "Female", "Other"],
31 | validators=[Optional()]
32 | )
33 | dob = DateField(
34 | "Date of Birth",
35 | validators=[Optional()]
36 | )
37 | password = PasswordField(
38 | "Password",
39 | validators=[DataRequired(), Length(5, 25)]
40 | )
41 | confirm_password = PasswordField(
42 | "Confirm Password",
43 | validators=[DataRequired(), Length(5, 25), EqualTo("password")]
44 | )
45 | submit = SubmitField("Sign Up")
46 |
47 |
48 | class LoginForm(FlaskForm):
49 | email = StringField(
50 | "Email",
51 | validators=[DataRequired(), Email()]
52 | )
53 | password = PasswordField(
54 | "Password",
55 | validators=[DataRequired(), Length(5, 25)]
56 | )
57 | remember_me = BooleanField("Remember Me")
58 | submit = SubmitField("Login")
59 |
--------------------------------------------------------------------------------
/Session 4 - Web Forms & Input Validation/templates/home.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 | Contents:
5 |
9 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 4 - Web Forms & Input Validation/templates/layout.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | {{ title }}
7 |
8 |
9 | Welcome to the {{ title }} page!
10 |
11 | {% with messages = get_flashed_messages() %}
12 | {% if messages %}
13 | {% for message in messages %}
14 | {{ message }}
15 | {% endfor %}
16 | {% endif %}
17 | {% endwith %}
18 |
19 | {% block content %}
20 | {% endblock %}
21 |
22 |
--------------------------------------------------------------------------------
/Session 4 - Web Forms & Input Validation/templates/login.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 |
39 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 4 - Web Forms & Input Validation/templates/signup.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 |
73 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 5 - Databases/Session 5 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 5 - Databases/Session 5 - notes.pdf
--------------------------------------------------------------------------------
/Session 5 - Databases/db_crud_demo.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 | from flask_sqlalchemy import SQLAlchemy
3 |
4 | app = Flask(__name__)
5 | app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///employees_db.db" # URL of DB to establish connection
6 | app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False # to hide warnings by SQL Alchemy
7 |
8 | db = SQLAlchemy(app)
9 |
10 | # database models
11 | class Employee(db.Model):
12 | id = db.Column(db.Integer, primary_key=True)
13 | name = db.Column(db.String(50), nullable=False)
14 | age = db.Column(db.Integer, nullable=False)
15 | email = db.Column(db.String(50), nullable=False, unique=True)
16 |
17 | def __repr__(self):
18 | return f"Employee('{self.name}', {self.age}, '{self.email}')"
19 |
20 | # run the flask app
21 | if __name__ == "__main__":
22 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 5 - Databases/many_to_many.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 | from flask_sqlalchemy import SQLAlchemy
3 |
4 | app = Flask(__name__)
5 | app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///store.db" # URL of DB to establish connection
6 | app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False # to hide warnings by SQL Alchemy
7 |
8 | db = SQLAlchemy(app)
9 |
10 | # association table
11 | customer_product = db.Table(
12 | "customer_product",
13 | db.Column("customer_id", db.Integer, db.ForeignKey("customers.id")),
14 | db.Column("product_id", db.Integer, db.ForeignKey("products.id"))
15 | )
16 |
17 | # database models
18 | class Customer(db.Model):
19 | __tablename__ = "customers"
20 | id = db.Column(db.Integer, primary_key=True)
21 | name = db.Column(db.String(50), nullable=False)
22 | email = db.Column(db.String(50), nullable=False, unique=True)
23 | items = db.relationship("Product", backref="owners", secondary=customer_product)
24 |
25 | def __repr__(self):
26 | return f"Customer('{self.name}', '{self.email}')"
27 |
28 |
29 | class Product(db.Model):
30 | __tablename__ = "products"
31 | id = db.Column(db.Integer, primary_key=True)
32 | product = db.Column(db.String(50), nullable=False)
33 | price = db.Column(db.Integer, nullable=False)
34 |
35 | def __repr__(self):
36 | return f"Product('{self.product}', '{self.price}')"
37 |
38 |
39 | # run the flask app
40 | if __name__ == "__main__":
41 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 5 - Databases/one_to_many.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 | from flask_sqlalchemy import SQLAlchemy
3 |
4 | app = Flask(__name__)
5 | app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///ipl.db" # URL of DB to establish connection
6 | app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False # to hide warnings by SQL Alchemy
7 |
8 | db = SQLAlchemy(app)
9 |
10 | # database models
11 | class Team(db.Model):
12 | __tablename__ = "teams"
13 | id = db.Column(db.Integer, primary_key=True)
14 | team = db.Column(db.String(50), nullable=False, unique=True)
15 | state = db.Column(db.String(50), nullable=False)
16 | members = db.relationship("Player", backref="team")
17 |
18 | def __repr__(self):
19 | return f"Team('{self.team}', '{self.state}')"
20 |
21 | class Player(db.Model):
22 | __tablename__ = "players"
23 | id = db.Column(db.Integer, primary_key=True)
24 | name = db.Column(db.String(50), nullable=False)
25 | nationality = db.Column(db.String(50), nullable=False)
26 | team_id = db.Column(db.Integer, db.ForeignKey("teams.id"))
27 |
28 | def __repr__(self):
29 | return f"Player('{self.name}', '{self.nationality}')"
30 |
31 |
32 | # run the flask app
33 | if __name__ == "__main__":
34 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 6 - Sessions/Session 6 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 6 - Sessions/Session 6 - notes.pdf
--------------------------------------------------------------------------------
/Session 6 - Sessions/client_sessions.py:
--------------------------------------------------------------------------------
1 | from flask import (
2 | Flask,
3 | render_template,
4 | redirect,
5 | url_for,
6 | flash,
7 | session,
8 | request
9 | )
10 | from forms import LoginForm
11 |
12 | app = Flask(__name__)
13 | app.config["SECRET_KEY"] = "secret_key"
14 |
15 | @app.route("/")
16 | @app.route("/home")
17 | def home():
18 | return render_template("home.html", title="Home")
19 |
20 |
21 | @app.route("/about")
22 | def about():
23 | if "user_name" not in session:
24 | flash("Login Required!")
25 | return redirect(url_for('login', next=request.url))
26 | else:
27 | flash(f"Hi {session["user_name"]}, have a good day!")
28 | return render_template("about.html", title="About")
29 |
30 |
31 | @app.route("/contact")
32 | def contact():
33 | if "user_name" not in session:
34 | flash("Login Required!")
35 | return redirect(url_for('login', next=request.url))
36 | else:
37 | flash(f"Hi {session["user_name"]}, have a good day!")
38 | return render_template("contact.html", title="Contact")
39 |
40 |
41 | @app.route("/login", methods=["GET", "POST"])
42 | def login():
43 | form = LoginForm()
44 | if form.validate_on_submit():
45 | session["user_name"] = form.username.data
46 | flash(f"Successfully logged in as {session["user_name"].title()}!")
47 | next_url = request.args.get("next")
48 | return redirect(next_url or url_for("home"))
49 | return render_template("login.html", title="Login", form=form)
50 |
51 |
52 | if __name__ == "__main__":
53 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 6 - Sessions/flask_session/2029240f6d1128be89ddc32729463129:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 6 - Sessions/flask_session/2029240f6d1128be89ddc32729463129
--------------------------------------------------------------------------------
/Session 6 - Sessions/flask_session/677e546b8071aa256de8ee836ffb22f5:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 6 - Sessions/flask_session/677e546b8071aa256de8ee836ffb22f5
--------------------------------------------------------------------------------
/Session 6 - Sessions/forms.py:
--------------------------------------------------------------------------------
1 | from flask_wtf import FlaskForm
2 | from wtforms import StringField, PasswordField, SubmitField
3 | from wtforms.validators import DataRequired
4 |
5 |
6 | class LoginForm(FlaskForm):
7 | username = StringField("Username", validators=[DataRequired()])
8 | password = PasswordField("Password", validators=[DataRequired()])
9 | submit = SubmitField("Login")
10 |
--------------------------------------------------------------------------------
/Session 6 - Sessions/server_sessions.py:
--------------------------------------------------------------------------------
1 | from flask import (
2 | Flask,
3 | render_template,
4 | redirect,
5 | url_for,
6 | flash,
7 | session,
8 | request
9 | )
10 | from flask_session import Session
11 | from forms import LoginForm
12 |
13 | app = Flask(__name__)
14 | app.config["SECRET_KEY"] = "secret_key"
15 | app.config["SESSION_TYPE"] = "filesystem"
16 | Session(app)
17 |
18 | @app.route("/")
19 | @app.route("/home")
20 | def home():
21 | return render_template("home.html", title="Home")
22 |
23 |
24 | @app.route("/about")
25 | def about():
26 | if "user_name" not in session:
27 | flash("Login Required!")
28 | return redirect(url_for('login', next=request.url))
29 | else:
30 | flash(f"Hi {session["user_name"]}, have a good day!")
31 | return render_template("about.html", title="About")
32 |
33 |
34 | @app.route("/contact")
35 | def contact():
36 | if "user_name" not in session:
37 | flash("Login Required!")
38 | return redirect(url_for('login', next=request.url))
39 | else:
40 | flash(f"Hi {session["user_name"]}, have a good day!")
41 | return render_template("contact.html", title="Contact")
42 |
43 |
44 | @app.route("/login", methods=["GET", "POST"])
45 | def login():
46 | form = LoginForm()
47 | if form.validate_on_submit():
48 | session["user_name"] = form.username.data
49 | flash(f"Successfully logged in as {session["user_name"].title()}!")
50 | next_url = request.args.get("next")
51 | return redirect(next_url or url_for("home"))
52 | return render_template("login.html", title="Login", form=form)
53 |
54 |
55 | if __name__ == "__main__":
56 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 6 - Sessions/templates/about.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
--------------------------------------------------------------------------------
/Session 6 - Sessions/templates/contact.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
--------------------------------------------------------------------------------
/Session 6 - Sessions/templates/home.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 |
11 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 6 - Sessions/templates/layout.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | {{ title }}
7 |
8 |
9 | Welcome to the {{ title }} page!
10 |
11 | {% with messages = get_flashed_messages() %}
12 | {% if messages %}
13 | {% for message in messages %}
14 | {{ message }}
15 | {% endfor %}
16 | {% endif %}
17 | {% endwith %}
18 |
19 | {% block content %}
20 | {% endblock %}
21 |
22 |
--------------------------------------------------------------------------------
/Session 6 - Sessions/templates/login.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 |
33 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 7 - Cookies/Session 7 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 7 - Cookies/Session 7 - notes.pdf
--------------------------------------------------------------------------------
/Session 7 - Cookies/demo_1.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, make_response, request
2 |
3 | app = Flask(__name__)
4 |
5 | @app.route("/")
6 | def home():
7 | response = make_response("Welcome to the Home Page!
")
8 | return response
9 |
10 |
11 | @app.route("/set_cookie")
12 | def set_cookie():
13 | response = make_response("Welcome to the Set Cookie Page!
")
14 | response.set_cookie("cookie_name", "cookie_value")
15 | return response
16 |
17 |
18 | @app.route("/get_cookie")
19 | def get_cookie():
20 | value = request.cookies.get("cookie_name")
21 | response = make_response(f"The cookie value is {value}!
")
22 | return response
23 |
24 |
25 | if __name__ == "__main__":
26 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 7 - Cookies/demo_2.py:
--------------------------------------------------------------------------------
1 | from flask import (
2 | Flask,
3 | render_template,
4 | redirect,
5 | url_for,
6 | flash,
7 | request,
8 | make_response
9 | )
10 | from forms import LoginForm
11 |
12 | app = Flask(__name__)
13 | app.config["SECRET_KEY"] = "secret_key"
14 |
15 | @app.route("/")
16 | @app.route("/home")
17 | def home():
18 | return render_template("home.html", title="Home")
19 |
20 |
21 | @app.route("/about")
22 | def about():
23 | user_name = request.cookies.get("user_name")
24 | if user_name is None:
25 | flash("Login Required!")
26 | return redirect(url_for('login', next=request.url))
27 | else:
28 | flash(f"Hi {user_name}, have a good day!")
29 | return render_template("about.html", title="About")
30 |
31 |
32 | @app.route("/contact")
33 | def contact():
34 | user_name = request.cookies.get("user_name")
35 | if user_name is None:
36 | flash("Login Required!")
37 | return redirect(url_for('login', next=request.url))
38 | else:
39 | flash(f"Hi {user_name}, have a good day!")
40 | return render_template("contact.html", title="Contact")
41 |
42 |
43 | @app.route("/login", methods=["GET", "POST"])
44 | def login():
45 | form = LoginForm()
46 | if form.validate_on_submit():
47 | user_name = form.username.data
48 | response = make_response("")
49 | response.set_cookie("user_name", user_name)
50 | flash(f"Successfully logged in as {user_name.title()}!")
51 | next_url = request.args.get("next") or url_for("home")
52 | response.headers["Location"] = next_url
53 | response.status_code = 302
54 | return response
55 | return render_template("login.html", title="Login", form=form)
56 |
57 |
58 | if __name__ == "__main__":
59 | app.run(debug=True)
--------------------------------------------------------------------------------
/Session 7 - Cookies/forms.py:
--------------------------------------------------------------------------------
1 | from flask_wtf import FlaskForm
2 | from wtforms import StringField, PasswordField, SubmitField
3 | from wtforms.validators import DataRequired
4 |
5 |
6 | class LoginForm(FlaskForm):
7 | username = StringField("Username", validators=[DataRequired()])
8 | password = PasswordField("Password", validators=[DataRequired()])
9 | submit = SubmitField("Login")
10 |
--------------------------------------------------------------------------------
/Session 7 - Cookies/templates/about.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
--------------------------------------------------------------------------------
/Session 7 - Cookies/templates/contact.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
--------------------------------------------------------------------------------
/Session 7 - Cookies/templates/home.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 |
11 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 7 - Cookies/templates/layout.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | {{ title }}
7 |
8 |
9 | Welcome to the {{ title }} page!
10 |
11 | {% with messages = get_flashed_messages() %}
12 | {% if messages %}
13 | {% for message in messages %}
14 | {{ message }}
15 | {% endfor %}
16 | {% endif %}
17 | {% endwith %}
18 |
19 | {% block content %}
20 | {% endblock %}
21 |
22 |
--------------------------------------------------------------------------------
/Session 7 - Cookies/templates/login.html:
--------------------------------------------------------------------------------
1 | {% extends "layout.html" %}
2 |
3 | {% block content %}
4 |
33 | {% endblock content %}
--------------------------------------------------------------------------------
/Session 8 - Training & Deloyment of ML Model/README.md:
--------------------------------------------------------------------------------
1 | ### In this session, we covered the following:
2 | - Training a machine learning model using Scikit-learn
3 | - Creation of an API to serve the model with Flask
4 | - Deployment of the model using Render
5 |
6 | ### The code files and the deployed web application for this session can be accessed through the below links:
7 | - [Code Files](https://github.com/MisbahullahSheriff/flask-course-ml-project)
8 | - [Web Application](https://flask-course-ml-project.onrender.com)
9 |
--------------------------------------------------------------------------------
/Session 8 - Training & Deloyment of ML Model/Session 8 - notes.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/campusx-official/flask-course/0461cefed80753f679899a4b5da59d5921cc92a2/Session 8 - Training & Deloyment of ML Model/Session 8 - notes.pdf
--------------------------------------------------------------------------------