Project Brief
Today’s task is different from what you are used to seeing so far. Your task is to publish the Flask app whose code is given further below.
Use PythonAnywhere for that. It is free to deploy websites on PythonAnywhere. The solution in the Project Solution section contains a step-by-step guide with screenshots showing you how to do the same.
Expected Output
The expected output is the website running on its own public URL:
Here is the code of the app above and which you need to deploy to PythonAnywhere:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return """
<html>
<head>
<title>Simple Flask Website</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">FlaskApp</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about">About</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="jumbotron mt-4">
<h1 class="display-4">Welcome to My Simple Flask Website</h1>
<p class="lead">This is a simple website with HTML embedded in the Python file.</p>
<hr class="my-4">
<p>It uses Bootstrap for styling and is built with Flask.</p>
<a class="btn btn-primary btn-lg" href="/about" role="button">Learn more</a>
</div>
</div>
</body>
</html>
"""
@app.route('/about')
def about():
return """
<html>
<head>
<title>About - Simple Flask Website</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">FlaskApp</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/about">About</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<div class="jumbotron mt-4">
<h1 class="display-4">About This Website</h1>
<p class="lead">This page is all about this simple Flask website.</p>
<hr class="my-4">
<p>It's styled using Bootstrap and serves as a basic example of a Flask web app.</p>
<a class="btn btn-primary btn-lg" href="/" role="button">Go back to Home</a>
</div>
</div>
</body>
</html>
"""
if __name__ == '__main__':
app.run(debug=True)
Environment Setup Instructions
Install the required libraries with:
pip install flask
Execute with python main.py and follow the instructions in the command line to use the program.
Project Solution
Feel free to post your questions or comments in the comment section below.
Happy Coding!
Daily Python Projects Team