Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Environment Configuration and Dependency Management | Introduction to the Course
Professional Web API with Flask
course content

Contenido del Curso

Professional Web API with Flask

Professional Web API with Flask

1. Introduction to the Course
2. Database and Models
3. Flask-Smorest and Schemas
4. Endpoints with Blueprints and MethodView
5. Authentication with JWT
6. Deployment

bookEnvironment Configuration and Dependency Management

New Branch

We've successfully activated our virtual environment, a practice highly recommended for all developers. It's advisable not to work directly on the main branch but rather on a development branch, such as dev or develop. You can create multiple branches for developing various features. For the duration of our project, we'll be working on the develop branch.

To initiate a new branch, execute the command:

— where the -b flag both creates and switches to the new branch, confirmed by the message 'Switched to a new branch develop'.

.env

Moving on, I'd like to discuss the importance of a '.env' file. This special file is where we securely store sensitive data such as SECRET_KEY, database passwords, and various API keys. To keep this information safe, create a '.env' file at the project's root and include our SECRET_KEY there. Additionally, create a '.env.sample' file to provide an example of the '.env' file's structure, omitting personal information.

To utilize the data within the '.env' file, we must install the python-dotenv package using the command:

Next, transfer the SECRET_KEY from the 'app.py' file to the '.env' file. Remember to also update the '.env.sample' file with a placeholder for the SECRET_KEY.

Back in the 'app.py' file, import the os package and

Initialize load_dotenv() before configuring our app, and replace the secret key with

enabling us to securely fetch the sensitive information from the '.env' file.

.gitignore

Another best practice is to utilize a .gitignore file. As we commit and push updates to the GitHub repository, certain files are unnecessary or potentially risky to include. By either opening or creating a .gitignore file and listing: venv .idea __pycache__ .env , we ensure these files and directories are ignored by GitHub.

requirements.txt

Execute

to generate this file, and periodically run this command to update it with any new packages we install. You'll notice the file automatically includes several packages, such as flask and python-dotenv.

Committing and Pushing Updates

Now, let's practice committing and pushing our new files and updates. It's advisable to do this more regularly than perhaps we have been. Choose to commit messages that clearly describe the update for easy understanding by yourself or colleagues in the future. For example, git commit -am 'Initialized flask app and added requirements.txt', using the past tense for verbs to detail commits. The flags 'a' and 'm' signify 'commit all new files and changes', respectively.

To push, use the command:

MacOS users might need to provide a username and password. If so, follow the instructions to generate a new SSH key, and remember to switch the HTTPS link to an SSH one in the .ssh/config file.

We've completed several important configurations that some developers overlook, but understanding how to work with these tools effectively is beneficial.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt