{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "S1QkB4XbClXs"
},
"source": [
"# 7. Create a Simple Flask App \n",
"\n",
"```{note} You can clone the source code [here](https://github.com/byambaa1982/python_cources_for_beginers/tree/main/Mini%20Project%202)\n",
"```\n",
"`Flask` is a micro web framework written in Python. It's considered a micro framework because it does not require particular tools or libraries, but it's expandable and can support complex applications as well. It has a very small and easy-to-understand code base, and provides only a few essential components out of the box, such as routing, request handling, and HTML templating. However, it's highly extensible and allows you to add more functionality through extensions and libraries, including database integration, form validation, authentication, and more.\n",
"\n",
"Here's why you might want to choose Flask for your web development project:\n",
"\n",
"1. `Simplicity and Ease of Use`: Flask is very simple to set up and use. It's a good choice if you're new to web development, if you're a solo developer, or if you're working on a small project. The Flask documentation is very comprehensive and easy to understand.\n",
"\n",
"2. `Flexibility`: Flask is very flexible. It doesn't make many decisions for you, which means you have the freedom to structure your application however you like and use whatever libraries or tools you prefer. This is a contrast to more \"batteries-included\" frameworks, like Django, which come with more built-in tools but are also more prescriptive.\n",
"\n",
"3. `Lightweight`: Flask is lightweight and has a small footprint, meaning it uses less system resources and can run efficiently on small servers.\n",
"\n",
"4. `Extensibility`: Even though Flask is lightweight and simple, it can be extended with various extensions to add functionalities such as SQL databases, authentication, form handling, and more. This makes it a good choice for both simple and complex projects.\n",
"\n",
"5. `Community and Ecosystem`: Flask has a large and active community of developers, so it's easy to find help when you need it. There are many tutorials, guides, and examples available, as well as a large number of libraries and extensions.\n",
"\n",
"6. `Integration with Python ecosystem`: Since Flask is a Python framework, you have access to the vast Python ecosystem and all its libraries for tasks such as data analysis, machine learning, and more.\n",
"\n",
"Overall, Flask provides a nice balance between simplicity, flexibility, and power, which makes it a popular choice for web development in Python.\n",
"Creating a Flask application usually requires two primary files: an app.py file which will contain your application's code, and a requirements.txt file to list any dependencies your application might have. In this case, our Flask application will be very basic and will just display a \"Hello, World!\" message when someone accesses it.\n",
"\n",
"```\n",
"python_cources_for_beginners/\n",
"└── Mini Project 2/\n",
" ├── requirements.txt\n",
" └── app.py\n",
"\n",
"```\n",
"\n",
"Here's what those files might look like:\n",
"\n",
"
\n",
"\n",
"1. `app.py`:\n",
"\n",
"``` python\n",
"from flask import Flask\n",
"app = Flask(__name__)\n",
"\n",
"@app.route('/')\n",
"def hello_world():\n",
" return 'Hello, World!'\n",
"\n",
"if __name__ == '__main__':\n",
" app.run(debug=True)\n",
"\n",
"```\n",
"
\n",
"\n",
"This is a very simple Flask application. The `app = Flask(__name__)` line creates an instance of the Flask class for our application. The `@app.route('/')` line is a decorator that Flask provides to route web requests to particular functions. The hello_world function is mapped to the root URL ('/') and returns the string `'Hello, World!'`. Finally, `app.run(debug=True)` runs the application (in debug mode in this case).\n",
"\n",
"
\n",
"2. `requirements.txt`: \n",
"\n",
"```python\n",
"Flask ==2.0.1\n",
"```\n",
"
\n",
"\n",
"This file lists Flask as a dependency for our application. This is useful if we're sharing our code with others or deploying it to a production environment. Someone can use the command `pip install -r requirements.txt` to install all the dependencies listed in this file.\n",
"\n",
"Please replace `Flask==2.0.1` with the version of Flask we are currently using.\n",
"\n",
"We can run the application locally by using the terminal and typing `python app.py`, assuming you have Python and Flask installed. We can see output telling us that a server is running locally, and we can access it by opening a web browser and navigating to `http://127.0.0.1:5000/` or `http://localhost:5000/`. We should see 'Hello, World!' displayed."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "gtZe_w8RD08A"
},
"source": [
"## Render HTML content in Flask\n",
"\n",
" To render HTML content in Flask, we typically use templates. Flask configures the Jinja2 template engine by default. Let's add an HTML file to our project:\n",
"\n",
"First, create a new directory named `templates` in our project's root directory. Flask looks for templates in this directory by default.\n",
"\n",
"Then, create a new HTML file in this directory. Let's call it `index.html`:\n",
"\n",
"```html\n",
"\n",
"\n",
"\n",
"
{{ message }}
\n", "