Python 35
Web Development with Flask
What is web-browser? (Chrome/edge/Firefox)
How a Web browser works
User --> browser-->Web server--> Browser-->User
A Web Browser can :
Send HTTP requests(GET,POST)
Receive responses from servers
Render HTML and apply CSS Styles
Java Script
Display Images ,videos ,text
What is web-server? (Apache/NGNX/IIS/Gunicorn/Tomcat)
A Web server is a software(and sometime hardware) that requests from a browser and send responses back(HTML,JSON,Images,etc.)
It has completed setup, which page to go all software code will be exists
Browser -->request -->webserver (routing)-->response-->Browser)
Web servers (selected List)
Web server Plant/Language Used For
1. Apach HTTP server --> Cross-plantform static & dynamic websites
2. Nginx Cross-platform High-traffic websites
3. Microsoft IIS Windows .Net web applications
4. Gunicorn Python Flask/Django production apps
5.Apache tomcat Java Java Web applications
Introduction to Flask
It is web framework it will take request from webserver and send response to browser. light weight frame work ,high weight django
-->Flask is a lightweight web framework written in python
-->It helps developer build web applications and APIS easily
It simple words:
Flask helps Python programs talk to the browser.
pip install flask
Why do we need flask?
If you want to print a message using python, there are multiple ways.
1.Print in the terminal.
def index():
print("Welcome to ccit")
2.Print in the web-page
def index():
return "Welcome to ccit"
Setting Up a Flask Project
Task1:
app.py:
from flask import Flask
"""
Create a Flask application instance.
__name__ is a special Python variable that gets the name of the current module.
When the script is run directly, __name__ equals "__main__".
"""
app = Flask(__name__)
"""
Define a route for the root URL ('/').
The @app.route decorator tells Flask what URL should trigger this function.
'GET' method is the default, so methods=['GET'] is optional.
"""
@app.route('/')
def home():
"""Handle requests to the root URL and return a welcome message."""
return "Welcome to the Flask Web Application!"
"""
Check if this script is executed directly (not imported as a module).
If so, run the Flask development server with debug mode enabled.
"""
if __name__ == '__main__':
# Debug mode provides auto-reload and detailed error pages
app.run(debug=True, port=5000)
* Running on http://127.0.0.1:5000
Display test in web page :
Welcome to the Flask Web Application!
Task2: Render template
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True, port=5000)
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cloud Computing In Telugu</title>
<link rel="stylesheet" href="static/style.css">
</head>
<body>
<nav >
<ul class="menu">
<li><a href = "/">Home</a></li>
<li><a href="/send">Send</a></li>
<li><a href="/Student">Student</a></li>
</ul>
</nav>
<!-- Header Section -->
<header>
<h1>AWS Cloud Computing</h1>
<section>
<p>Learn how websites are built from scratch</p>
</header>
<main>
<section id="sec4">
<p>
This training is suitable for students, beginners, and working professionals.
Anyone interested in building websites or starting a career in web development can join.
</p>
</section>
</main>
<!-- Footer -->
<footer>
<p>Regards,</p>
<p>
<a href="https://www.Google.com" target="_blank">
Google Page
</a>
</p>
</footer>
</body>
</html>
Web page Output:
Above one example default method Get
Task3:
We have different type of method in routing get/put/post/delete
While click any button and expecting response is called post method
app.py
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html', title="Home Page")
@app.route('/', methods=['POST'])
def send():
topics = """
<h2>Topics Covered in This Training</h2>
<ul>
<li>Introduction to Cloud Computing</li>
<li>AWS Services Overview</li>
<li>Deploying Applications on AWS</li>
<li>Managing AWS Resources</li>
<li>Security Best Practices</li>
</ul>
"""
return render_template('index.html', title="Home Page", msg=topics)
if __name__ == '__main__':
app.run(debug=True)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cloud Computing In Telugu</title>
<link rel="stylesheet" href="static/style.css">
</head>
<body>
<nav >
<ul class="menu">
<li><a href = "/">Home</a></li>
<li><a href="/send">Send</a></li>
<li><a href="/Student">Student</a></li>
</ul>
</nav>
<!-- Header Section -->
<header>
<h1>AWS Cloud Computing</h1>
<section>
<p>Learn how websites are built from scratch</p>
</header>
<main>
<section id="sec4">
<form method="post" >
<button type="submit">Enroll Now</button>
{% if msg %}
{{ msg|safe }}
{% endif %}
</form>
<h2>Join Our Cloud Computing Course Today!</h2>
</section>
</main>
<!-- Footer -->
<footer>
<p>Regards,</p>
<p>
<a href="https://www.Google.com" target="_blank">
Google Page
</a>
</p>
</footer>
</body>
</html>
When click button request send to Post metod ,display below text
Web development basics:
We need to create an object of the flask class(app=Flask(_name_)
A route(@app.route("/")) guides the Flash framework to execute a particular python function when a specific URL is accessed
(If _name_ =="_main_":)This block ensures that the Flask application runs only when the file is executed directly, not when it is imported
(app.run(debug=true))starts the Flask development server
Handling Routes:
Routing navigation the one page to another
Routing maps URLs to specific functions (views) in your application. When a user visits a URL, Flask calls the corresponding function to handle that request.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cloud Computing In Telugu</title>
<link rel="stylesheet" href="static/style.css">
</head>
<body>
<nav >
<ul class="menu">
<li><a href = "/">Home</a></li>
<li><a href="/send1">Send</a></li>
<li><a href="/student">Student</a></li>
</ul>
</nav>
<!-- Header Section -->
<header>
<h1>AWS Cloud Computing</h1>
<section>
<p>Learn how websites are built from scratch</p>
</header>
<main>
<section id="sec4">
<form method="post" >
<button type="submit">Enroll Now</button>
{% if msg %}
{{ msg|safe }}
{% endif %}
</form>
<h2>Join Our Cloud Computing Course Today!</h2>
</section>
</main>
<!-- Footer -->
<footer>
<p>Regards,</p>
<p>
<a href="https://www.Google.com" target="_blank">
Google Page
</a>
</p>
</footer>
</body>
</html>
app.py
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html', title="Home Page")
@app.route('/send1')
def send1():
return "Thank you for enrolling!"
@app.route('/student')
def student():
return "Thank you for student enrolling!"
@app.route('/', methods=['POST'])
def send():
topics = """
<h2>Topics Covered in This Training</h2>
<ul>
<li>Introduction to Cloud Computing</li>
<li>AWS Services Overview</li>
<li>Deploying Applications on AWS</li>
<li>Managing AWS Resources</li>
<li>Security Best Practices</li>
</ul>
"""
return render_template('index.html', title="Home Page", msg=topics)
if __name__ == '__main__':
app.run(debug=True)
When click Send
It will call send tab it called send1 function
Same way when click student tab it will call student function
--Thanks