[go: up one dir, main page]

0% found this document useful (0 votes)
3 views3 pages

Script

This document provides a tutorial on setting up a basic web server using Flask, a lightweight Python web framework. It covers the installation of Flask, the creation of a Flask application, defining routes, and rendering HTML templates. The tutorial emphasizes the ease of use for beginners while highlighting Flask's capabilities for building both simple and complex web applications.

Uploaded by

leon.jibin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Script

This document provides a tutorial on setting up a basic web server using Flask, a lightweight Python web framework. It covers the installation of Flask, the creation of a Flask application, defining routes, and rendering HTML templates. The tutorial emphasizes the ease of use for beginners while highlighting Flask's capabilities for building both simple and complex web applications.

Uploaded by

leon.jibin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

"Hey guys! Welcome back to the channel!

Today, we're diving into something super


cool—I'll show you how to set up a basic web server with Flask and render an HTML
template. Perfect if you're just getting into web development with Python. Let’s
jump right in!"

"Alright, first up, we need to make sure Flask is installed. Open up your command
prompt or terminal and type in the following command: pip install Flask "

"Here, we’re telling Python’s package manager, pip, to install Flask. Flask is the
library we'll use to set up our web server. Pip is basically the tool that helps us
install different Python modules or libraries whic we mentioned in the previos
video.
Once Flask is installed, you’re all set to start coding!"

"Flask is a lightweight and flexible Python web framework that makes it super easy
to build web applications and APIs. It’s often referred to as a ‘micro-framework’
because it doesn’t come with a lot of built-in tools—this means you only include
what you need, keeping your project simple and fast."

"Flask is popular because it’s beginner-friendly, yet powerful enough for real-
world applications. It handles the basics of web development, like routing (which
determines what happens when users visit different URLs) and rendering HTML
templates. You can build everything from small projects to large-scale applications
with it!"

Alright now lets start writing the actual code. I will explain everything in
detail.

from flask import Flask, render_template:

This line imports two important parts of Flask:


Flask: This is the main class we use to create our Flask application. Creating an
instance of Flask essentially sets up our app, allowing it to handle requests and
responses.
render_template: This function is used to render HTML templates. It takes an HTML
file (usually from a folder named templates) and sends it to the user’s web
browser. This lets us serve up full HTML pages, rather than just plain text.
python
app = Flask(__name__):

Here, we create an instance of the Flask class and store it in a variable named
app. This app object represents our Flask application and will be used to set up
routes and run the server.
__name__ is a special Python variable that represents the name of the current
module (in this case, our script). Flask uses it to understand where to look for
other files and resources, like templates and static files.

@app.route("/"):

This line is a route decorator, meaning it defines a route, or URL, for our app.
"/" is the URL path for this route. In Flask, "/" represents the root URL, which is
typically the homepage. So when someone visits our website’s root (like
http://127.0.0.1:5000/), Flask will execute the function right below this line.
In general, routes tell Flask, “If a user goes to this specific URL, execute the
following code.”

def home()::

Here, we’re defining a function called home(). This function will run whenever
someone visits the root URL (/). Functions associated with routes are often called
view functions in Flask.

return render_template("index.html"):

render_template("index.html") loads an HTML file named index.html from the


templates folder.
Flask knows to look for index.html in a folder named templates because that’s the
default location for templates in Flask.
return sends this rendered HTML page back to the user’s web browser, displaying the
page content.

if __name__ == "__main__": ensures that app.run() only runs if this script is


executed directly (like python app.py), and not if it’s imported as a module.
app.run(debug=True) starts the server and activates debug mode, making it easier to
develop and troubleshoot your app.
Now Lets Create The .html File which is Your Frontend Interface. Lets name it
index.html.

"This is the file that users actually see when they visit your site. It sets up the
layout, text, images, and all the other elements on the page. HTML is the core
language for web pages, giving structure to everything you display."

The <!DOCTYPE html> and <html> tags tell the browser that this is an HTML5
document.
The <head> section contains metadata, like the page title, which appears on the
browser tab.
The <body> section is where the main content goes. We have an <h1> header and a
paragraph (<p>), which will be displayed on our page."

Now lets see How They Work Together

"By using render_template() in app.py, we’re telling Flask to find index.html in a


folder called templates, load it, and send it to the browser whenever someone
visits the homepage. Flask uses the .py file for server logic and data processing,
while the .html file provides the structure and design."

"With Flask, you can build more complex projects by adding more routes and HTML
templates. You can even use CSS and JavaScript files to style and add interactivity
to your pages, turning Flask into a powerful tool for web development!"

You might also like