My First Web Application with Flask: A Beginner's Guide!
Microsoft Designer

My First Web Application with Flask: A Beginner's Guide!

Hi Friends,

This week, I ventured into the world of Flask, a dynamic framework for creating web applications.

While my professional expertise is rooted in cloud solution architecture, my curiosity has led me to explore Python and application development.

As I continue to expand my knowledge towards AI, I wanted to share my insights with those who share similar interests.

Let's dive in!

Flask Overview:

Flask is a powerful and versatile web framework used for building web applications, APIs, and machine learning applications. Developed by Armin Ronacher,

Flask is written in Python and is known for its simplicity and ease of use, making it a popular choice among developers.

Whether you're creating a personal blog, a social networking site, or deploying machine learning models, Flask provides the necessary tools to get the job done.

Key Components:

Below are the two key components you must be aware off-

  • WSGI (Web Server Gateway Interface): Flask is built on the WSGI protocol, which facilitates communication between web applications and web servers.
  • Jinja2 Template Engine: Flask uses Jinja2, a powerful templating engine in Python, to dynamically render web pages using data from various sources like ML models, SQL, MongoDB, and PostgreSQL.

Setting Up Flask in Visual Studio Code:

  • Anaconda Prompt: I am using anaconda prompt to open VS code in a new environment for this python application.

Article content
Create new environment & activate

  • Installing Flask: Flask is a lightweight framework that can be installed using pip:

Article content
Install flask

It's always a best practice to maintain separate environments for different projects in python to avoid conflicts and ensure clean setups. Here, I have created new environment myflaskenv for demo.

  • Python Code:

Just to showcase how we can call the flask class , create an instance and call the application by below python code

#Import flask class from Flask module
from flask import Flask

# Step 1: Create an instance of the Flask class, which represents the web application object "flaskapp". 
# The __name__ parameter allows the Flask application to know its location and handle resources properly.
flaskapp=Flask(__name__) 

#Step#2- Define routes using decorators. The @flaskapp.route decorator maps URLs to functions.
# When a user accesses the specified URL, the corresponding function is automatically triggered.
@flaskapp.route('/')

# Define a function that will be triggered when the root URL ("/") is accessed.
def welcome():
    return "Hey, its my first web application using flask!"

#Example of another url
@flaskapp.route('/preetha')
# Define a function that will be triggered when the "/preetha" URL is accessed.
def member():
    return "Welcome preetha"


#The block below ensures that certain initialization code, like starting the Flask server,only runs when the script is executed directly, giving you control over the script's behavior.

if __name__=='__main__':
    flaskapp.run()         

  • Run the python code

Article content
Run python code

Click on follow link,


Article content
URL "/"



Article content
URL /preetha

Conclusion:

Flask is a lightweight and flexible framework that simplifies the development of web applications, APIs, and machine learning solutions. Its integration with WSGI and Jinja2 makes it a powerful tool for creating dynamic and efficient web projects. By setting up Flask in Visual Studio Code and following best practices, you can create and manage various applications with ease. Embrace Flask's capabilities to bring your ideas to life and build robust web solutions.

With Flask, you can seamlessly create and manage various web applications and APIs while leveraging Python's capabilities. This setup guide helps you get started with your first Flask project effectively.

Enjoy learning and sharing 😊

Thank You All 😊

To view or add a comment, sign in

More articles by Preetha R.

Insights from the community

Others also viewed

Explore topics