When it comes to starting new FastAPI projects, the boilerplate setup can often be a tedious and error-prone process. Developers looking to streamline their workflow will find that the cookiecutter-fastapi template significantly reduces the time and effort needed to scaffold a new project. Whether you're building a machine learning application, implementing CI/CD with GitHub Actions, or running tests with Pytest, this template has you covered.
What Is Cookiecutter-FastAPI?
Cookiecutter-FastAPI is a powerful Cookiecutter template designed specifically for FastAPI projects. It leverages the Cookiecutter CLI tool to create a structured project layout, complete with essential configurations and best practices. The template supports a variety of features that are common in modern web applications, making it a great starting point for developers.
Key Features
- Machine Learning Ready: Seamlessly integrate machine learning components with FastAPI, setting up data pipelines and models effortlessly.
- GitHub Actions Integration: Preconfigured CI/CD workflows to automate testing and deployment processes right from the start.
- Testing with Pytest: Includes setup for Pytest, allowing you to write and run tests easily, ensuring your application remains robust.
- Scalable Architecture: Follows best practices in project structure, making it easier to scale your application as it grows.
- Customizable Templates: Use Jinja2 templating to easily modify folder and file names, or customize file content as needed.
- Support for Black: Automatically format your code with Black, ensuring that your code adheres to the PEP 8 style guide.
- Quick Start: Get your project up and running with minimal setup required. Just run a single command!
Installation & Setup
Getting started with cookiecutter-fastapi is simple. First, ensure you have Cookiecutter installed. You can do this using pip:
pip install cookiecutter
Once you have Cookiecutter installed, you can generate a FastAPI project by running the following command:
cookiecutter gh:arthurhenrique/cookiecutter-fastapi
Follow the prompts in the command line to customize your project setup. Once completed, you will have a fully structured FastAPI project ready for development.
How to Use It
Letβs walk through a practical example of using the cookiecutter-fastapi template to set up a simple FastAPI application:
- Open your terminal and run:
- Provide the necessary inputs when prompted, such as project name, author, etc.
- Navigate into your new project directory:
- Start your FastAPI application:
- Visit http://127.0.0.1:8000/docs to see the automatically generated API documentation.
cookiecutter gh:arthurhenrique/cookiecutter-fastapi
cd your_project_name
uvicorn app.main:app --reload
That's it! You've set up a brand new FastAPI project with all the essential features in just a few commands.
Who Should Use Cookiecutter-FastAPI?
This template is perfect for:
- Developers looking to quickly prototype FastAPI applications.
- Machine learning practitioners wanting a robust backend to serve their models.
- Teams interested in implementing CI/CD practices using GitHub Actions.
- Anyone who wants to start a new FastAPI project with best practices in mind.
Final Thoughts
In my experience, cookiecutter-fastapi has been an invaluable tool for setting up FastAPI projects. It saves time and reduces the friction often associated with starting new applications. With its focus on best practices and modern development workflows, I highly recommend it to anyone embarking on a FastAPI journey. Whether you're building APIs for machine learning models or just looking to create a streamlined web service, this template will set you on the right path.