1 | +2 | +3 | +
4 | +5 | +6 | +
7 | +8 | +9 | +
{{ content }}
+ + + ``` + +- **Render the Template**: + ```python + from flask import Flask, render_template + + app = Flask(__name__) + + @app.route('/') + def home(): + return render_template('index.html', title='Home', heading='Welcome to Flask', content='This is a Flask application.') + ``` + +### Serving Static Files +Static files like CSS, JavaScript, and images are placed in the `static` directory. + +- **Create Static Files**: + - `app/static/style.css`: + ```css + body { + font-family: Arial, sans-serif; + } + ``` + +- **Include Static Files in Templates**: + ```html + + + +{{ content }}
+ + + ``` + +## 7. Working with Forms +### Handling Form Data +Forms are used to collect user input. Flask provides utilities to handle form submissions. + +- **Create a Form**: + - `app/templates/form.html`: + ```html + + + +The page you are looking for does not exist.
+ + + + + + - **app/templates/500.html:** + + + + +Something went wrong on our end. Please try again later.
+ + + + +## 10. Testing Your Application +Flask applications can be tested using Python's built-in `unittest` framework. + +- **Write a Test Case**: + - `tests/test_app.py`: + ```python + import unittest + from app import create_app + + class BasicTestCase(unittest.TestCase): + def setUp(self): + self.app = create_app() + self.app.config['TESTING'] = True + self.client = self.app.test_client() + + def test_home(self): + response = self.client.get('/') + self.assertEqual(response.status_code, 200) + self.assertIn(b'Hello, World!', response.data) + + if __name__ == '__main__': + unittest.main() + ``` + + - **Run the Tests**: + ``` + python -m unittest discover -s tests + ``` + +## 11. Deploying Your Flask Application +### Using Gunicorn +Gunicorn is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model, meaning that it forks multiple worker processes to handle requests. + +- **Install Gunicorn**: + ``` + pip install gunicorn + ``` + +- **Run Your Application with Gunicorn**: + ``` + gunicorn -w 4 run:app + ``` + +### Deploying to Render +Render is a cloud platform for deploying web applications. + +- **Create a `requirements.txt` File**: + ``` + Flask + gunicorn + flask_sqlalchemy + ``` + +- **Create a `render.yaml` File**: + ```yaml + services: + - type: web + name: my-flask-app + env: python + plan: free + buildCommand: pip install -r requirements.txt + startCommand: gunicorn -w 4 run:app + ``` + +- **Deploy Your Application**: + 1. Push your code to a Git repository. + 2. Sign in to Render and create a new Web Service. + 3. Connect your repository and select the branch to deploy. + 4. Render will automatically use the `render.yaml` file to configure and deploy your application. + +## 12. Conclusion +Flask is a powerful and flexible framework for building web applications in Python. It offers simplicity and ease of use, making it a great choice for both beginners and experienced developers. This guide covered the basics of setting up a Flask application, routing, templating, working with forms, integrating databases, error handling, testing, and deployment. + +## 13. Further Reading and Resources +- Flask Documentation: https://flask.palletsprojects.com/en/latest/ +- Jinja2 Documentation: https://jinja.palletsprojects.com/en/latest/ +- SQLAlchemy Documentation: https://docs.sqlalchemy.org/en/latest/ +- Render Documentation: https://render.com/docs diff --git a/contrib/web-scrapping/index.md b/contrib/web-scrapping/index.md index 82596a2..276014e 100644 --- a/contrib/web-scrapping/index.md +++ b/contrib/web-scrapping/index.md @@ -1,3 +1,4 @@ # List of sections - [Section title](filename.md) +- [Introduction to Flask](flask.md)