Subscribe Us

How to create a Simple Web App Using Python ?

 






Step 1: Set Up Your Development Environment

  1. Install Python: Make sure you have Python installed on your system. You can download it from the official Python website.
  2. Install Flask: Use pip to install Flask. Open your terminal or command prompt and run:
    bash
    pip install Flask

Step 2: Create a Basic Flask Application

  1. Create a Project Directory: Create a new directory for your project and navigate into it.
    bash
    mkdir my_flask_app cd my_flask_app
  2. Create the Application File: Create a new file named app.py and open it in your text editor.

Step 3: Write Your Flask Application Code

  1. Import Flask: Import the Flask class from the flask module.
  2. Create an Instance of Flask: Create an instance of the Flask class.
  3. Define Routes: Use the @app.route decorator to define the routes and their corresponding handler functions.
  4. Run the App: Use the app.run method to run the app.

Here's the complete code for a basic Flask application:

python
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Flask!" if __name__ == '__main__': app.run(debug=True)

Step 4: Run Your Flask Application

  1. Run the Application: In your terminal, navigate to the directory containing app.py and run:
    bash
    python app.py
  2. Open Your Browser: Open your web browser and go to http://127.0.0.1:5000/. You should see the message "Hello, Flask!".

Step 5: Add More Functionality

You can add more routes and functionality to your web app. Here's an example of how to add more routes and use HTML templates:

  1. Create Templates Directory: Create a directory named templates in your project directory.

    bash
    mkdir templates
  2. Create an HTML Template: Create a new HTML file named index.html inside the templates directory.

    html
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Flask App</title> </head> <body> <h1>Welcome to My Flask App</h1> <p>{{ message }}</p> </body> </html>
  3. Update app.py to Render the Template:

    python
    from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('index.html', message="Hello, Flask with Templates!") @app.route('/about') def about(): return "This is the about page." if __name__ == '__main__': app.run(debug=True)

Step 6: Add More Routes and Templates

You can continue to add more routes and corresponding templates to expand your web app's functionality. Here's an example of adding an "About" page with a template:

  1. Create a New Template: Create an about.html file in the templates directory.

    html
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>About</title> </head> <body> <h1>About My Flask App</h1> <p>This is a simple web app built with Flask.</p> </body> </html>
  2. Update app.py to Render the New Template:

    python
    @app.route('/about') def about(): return render_template('about.html')

Step 7: Run and Test Your Application

  1. Run the Application: Run your Flask application again using:
    bash
    python app.py
  2. Test the Routes: Open your browser and navigate to http://127.0.0.1:5000/ for the home page and http://127.0.0.1:5000/about for the about page.

Post a Comment

0 Comments