Skip to content

Practice Exercise 6: Flask

Objectives

Create a simple Flask app

Tasks

  • Before doing this stop make sure to stop the nginx service
  • On the web1 instance, create a directory for your app and navigate into it:

    mkdir my_flask_app
    cd my_flask_app
    

  • Create a virtual environment

    python3 -m venv flask_env
    

  • Activate the virtual environment

    source flask_env/bin/activate
    

  • Install Flask in the virtual environment:

    pip install flask
    

  • Create a Python file for your Flask app app.py:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return 'Hello, and welcome to my first Flask app!'
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=80)
    

  • Start the flask app:

    sudo flask_env/bin/python3 app.py
    

  • Open a web browser and visit your host .a1t-inf-ds-web1.acad.opswerks.net

Note: Ensure port 80 is open

Conclusion

By following these steps, you have gained hands-on experience in creating a basic Flask web application, which can serve as a foundation for developing more complex web applications. Flask is a versatile and user-friendly web framework that provides a powerful platform for building web services and web-based applications.