Open In App

Run the fast-api server using Pycharm

Last Updated : 04 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

FastAPI is a Python web framework that makes it easy to build APIs quickly and efficiently. Returning an image is a common requirement in web development, and FastAPI makes it straightforward to do. another famous framework for doing the same is Flask, which is also Python-based.

Pre-requisite

Steps to Run Fast-API Server using PyCharm

Below are the steps by which we can run the fast-API server using PyCharm:

Step 1: Installation

For running the server we need a uvicorn. so before starting our API coding part, we need to the installation of fastAPI and uvicorn. we can install them by running the below command

pip install fastapi
pip install uvicorn

Step 2: Import FastAPI

First of all, create a new PyCharm project and then one folder with the name you want, eg FastAPI, inside that folder create a file with the name ‘main.py’ and import the library.

Python3




# Import required Library
from fastapi import FastAPI


Step 3: Create a FastAPI App

Now to use fastAPI we need to create it’s app, it’s very simpe as we just need to call FastAPI(). Write it inside the main.py itself.

Python3




# creating fastAPI app
app = FastAPI()


Step 4: Define a Route to Serve a response to the user

Here we define a route ‘/’ using the @app.get decorator. This route will handle GET requests. In this step we need to make route to our fastapi app. Here when any API request will come to our fastapi app first of all it will try to match with the url we mentioned inside app.get(), here get because client want the image in response so, client will send the get request. Here we had created 2 API endpoint for testing purpose only.

On user hitting the “/” we will send user a response object {“Response”: “simple FastAPI response”}, while hitting on url “/data/” we send user a response object {“name”: “GeeksForGeeks”,”url”: “https://practice.geeksforgeeks.org/”}

Python3




# Define a route to serve a user
@app.get("/")
def read_root():
    return {"Response": "simple FastAPI response"}
 
 
# another route to serve a user
@app.get("/data/")
def read_data():
    return {"name": "GeeksForGeeks",
            "url": "https://practice.geeksforgeeks.org/"}


Complete main.py File

Python3




# Import required Library
from fastapi import FastAPI
 
# creating fastAPI app
app = FastAPI()
 
 
# Define a route to serve a user
@app.get("/")
def read_root():
    return {"Response": "simple FastAPI response"}
 
 
# another route to serve a user
@app.get("/data/")
def read_data():
    return {"name": "GeeksForGeeks",
            "url": "https://practice.geeksforgeeks.org/"}


Step 5: Running and Deployment of the project

We can run the FastAPI app using a web server such as uvicorn:

uvicorn main:app --reload

Now, we can acess this API in browser by access ‘http://localhost:8000/’ or ‘http://127.0.0.1:8000/’ in your web browser or make a GET request to that URL using a tool like postman, the FastAPI app will return the a response object as we mentioned above in code.

Output

ezgifcom-resize

There is another method also to test our API by Postman tool by just sending GET request on “http://127.0.0.1:8000/” or “http://127.0.0.1:8000/data/”



Similar Reads

Create a new Django project in Pycharm using Pycharm Terminal
PyCharm is one of the most popular Python-IDE developed by JetBrains used for performing scripting in Python language. PyCharm provides many useful features like Code completion and inspection, Debugging process, support for various programming frameworks such as Flask and Django, Package Management, etc. PyCharm provides various tools for producti
2 min read
Fast API - Gunicorn vs Uvicorn
In this article, we will explore Gunicorn and Uvicorn in the context of FastAPI applications, examining two integral components essential for the deployment and execution of Python web services. What is FastAPI? FastAPI is a modern web framework specifically crafted for Python 3.8 and newer versions, leveraging standard Python-type hints for API cr
3 min read
Create a Pull Request on GitHub using Pycharm
Git is an open-source version control system. It means that whenever a developer develops some project (like an app or website) or something, he/she constantly updates it catering to the demands of users, technology, and whatsoever it maybe, Git is a version control system that lets you manage and keep track of your source code history. Find the ap
2 min read
Install Fastapi And Run Your First Fastapi Server On Windows
FastAPI is a modern, fast web framework for building APIs with Python 3.7+ based on standard Python-type hints. In this article, we'll walk through the process of installing FastAPI and creating a simple FastAPI server on a Windows system. Pre-Requisite: PythonFastAPIInstall And Run Python Fastapi Server On WindowsBelow is the step-by-step procedur
2 min read
How to Run Jupyter Notebooks from a Remote Server?
Jupyter Notebook is an open-source, interactive web application that allows you to write and run computer code in over 40 programming languages, including Python, R, Julia, and Scala. In this article, we will see how we can Jupyter Notebook with a configured server instead of localhost. Run Jupyter Notebooks from a Remote ServerBelow, is the step-b
2 min read
How to Install Python Pycharm on Linux?
Prerequisite: Python Language Introduction Python is a widely-used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code
2 min read
How to install NumPy in PyCharm?
In this article, we will cover how to install the Python NumPy package in PyCharm. NumPy is a general-purpose array-processing Python package. It provides a high-performance multidimensional array object, and tools for working with these arrays. As one of the most important tool while working with data manipulation, we often need to install NumPy p
2 min read
How to Upload Project on GitHub from Pycharm?
PyCharm is one of the most popular Python-IDE developed by JetBrains used for performing scripting in Python language. PyCharm provides some very useful features like Code completion and inspection, Debugging process, support for various programming frameworks such as Flask and Django, Package Management, etc. PyCharm provides various tools for pro
3 min read
Difference Between Jupyter and Pycharm
Jupyter notebook is an open-source IDE that is used to create Jupyter documents that can be created and shared with live codes. Also, it is a web-based interactive computational environment. The Jupyter notebook can support various languages that are popular in data science such as Python, Julia, Scala, R, etc. Pycharm is an IDE developed by JetBra
2 min read
Setup OpenCV With PyCharm Environment
Introduction:If you love working on image processing and video analysis using python then you have come to the right place. Python is one of the key languages which is used to process videos and images. Requirements for OpenCV:32- or a 64-bit computer.Windows, macOS or Linux.Python 2.7, 3.4, 3.5 or 3.6.PyCharmPyCharm is a cross-platform IDE used in
2 min read
Practice Tags :