Course Content
Professional Web API with Flask
Professional Web API with Flask
Enhancing Your App
Updating requirements.txt
To keep your Python application in top shape, it's essential to maintain an up-to-date requirements.txt file. This file tracks all external libraries your project depends on. As you develop and add new libraries, updating this file is as simple as running the command in your terminal:
Regularly updating this file ensures that other developers can set up their development environment more efficiently, mirroring your own setup.
Code Formatting with Black and Flake8
Maintaining a consistent coding style is crucial for the readability and maintainability of your project. Python developers have access to several tools to help enforce coding standards, with Black and Flake8 being among the most popular.
Black:
This tool automatically formats your Python code to adhere to a consistent style, making it more readable and reducing the time spent on discussing style in code reviews. Install Black using:
and run it across your project by simply executing the command:
Specify particular files or directories to format by appending their paths to the command. Black adjusts your code's syntax to meet its style guide, which includes preferences such as using double quotes over single quotes and ensuring proper spacing around operators and after commas.
Flake8:
While Black focuses on formatting, Flake8 is used to identify warnings about stylistic issues, programming errors, and other potential problems. It is a linting tool that helps you improve the quality of your code. Running:
It will display a list of warnings in the files of the models module that you can then manually address, such as reducing line lengths to comply with Python's PEP 8 guidelines, which recommend a maximum of 79 characters per line. Flake8 is invaluable for catching issues that may not affect your program's functionality but could lead to maintenance problems or readability issues down the line.
PyCharm built-in shortcuts
Additionally, development environments like PyCharm offer built-in shortcuts for code formatting, such as ⌘⌥L
(macOS) or Ctrl+Alt+L
(Windows/Linux). These shortcuts automatically reformat the code in the current file, organizing imports according to best practices and removing unused libraries, although they do not adjust line length. Reapplying these commands throughout your project can significantly enhance its overall code quality.
Crafting a Compelling README
The README file acts as the front door to your project on platforms like GitHub. It's the first thing other users, collaborators, and potential employers see, making it a powerful tool to communicate your project's purpose, setup instructions, and usage. Crafting a well-organized and informative README can significantly impact the perception of your project.
A good README includes:
- Project Title: a clear, concise title that gives an immediate idea of what the project does.
- Introduction: A brief description of the project's purpose and what it achieves.
- Installation Instructions: step-by-step guides to help users get your project running on their machines.
- Usage: examples of how to use your project, including code snippets and command-line instructions, if applicable.
- Contributing: Guidelines for how others can contribute to your project, if you're open to collaboration.
- License: Information on how your project is licensed, which affects how others can use it.
Seeking inspiration from well-documented repositories and leveraging templates can be beneficial. Remember, the goal of your README is not just to inform but also to engage and encourage users to try, use, and contribute to your project.
Here is the sample of the README.md from the video:
## FC Manager REST API
API service for management Football Clubs written on Flask REST API. With functionality managing teams and players.
Features:
- JWT authenticated
- Documentation is located via /swagger-ui/
- Implementing 4 models with many-to-many and many-to-one relationships
- Creating and updating teams and players of the club
### Installing using GitHub:
- Fork the project into your GitHub
- Clone it into your desktop
```
git clone https://github.com/your-link-to-the-project (please fill it)
cd fc_manager_api
```
- Set up virtual environment
```
python3 -m venv venv
source venv/bin/activate # for Unix-based system
venv\Scripts\activate # for Windows
```
- Install requirements
```
pip install -r requirements.txt
```
- Open .env.sample and change environment variables on yours! Rename the file from .env.sample to .env
### Run API service
```
flask migrate
flask run
```
### Getting access instruction:
- Create a user via **/register/**
- Get access token via **/login/**
- Install **ModHeader** browser extension and create a Request header with the value **Authorization Bearer** `<Your access token>`
Thanks for your feedback!