Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Simple FastAPI application

To get started with a FastAPI application, follow these steps:

1. Install FastAPI: 

First, make sure you have Python 3.6 or higher installed. Then, install FastAPI using pip:


pip install fastapi

 

2. Create a FastAPI App: 

Create a new Python file (e.g., main.py) and import FastAPI:


from fastapi import FastAPI

app = FastAPI()

 

3. Define a Route: Add a route to your app using a path operation decorator (e.g., @app.get("/")):


@app.get("/")

async def root():

    return {"message": "Hello World"}

 

4. Run the Development Server: Start the live server using the following command:


fastapi dev main.py

 

Access Your App:

Open your browser at http://127.0.0.1:8000 to see the JSON response: {"message": "Hello World"}.

 

Interactive API Docs:

Explore the automatic interactive API documentation at http://127.0.0.1:8000/docs (provided by Swagger UI).

 

Alternative API Docs:

Check out the alternative documentation at http://127.0.0.1:8000/redoc (provided by ReDoc).

 

OpenAPI Schema:

If you’re curious about the raw OpenAPI schema, view it directly at http://127.0.0.1:8000/openapi.json .

 

Happy coding!

How to create flask web application

How to create flask web application:

 

Creating a Flask web application is a straightforward process. Here are the basic steps to get you started:

 

1. Install Flask: First, make sure you have Python installed on your system. Then, install Flask using pip:

pip install flask

 

2. Create a Flask App: Create a new Python file (e.g., app.py) and import Flask:

from flask import Flask

app = Flask(__name__)

 

 3.  Define Routes: Use decorators to define routes for your app.

For example:

@app.route('/')

def home():

    return 'Hello, Flask!'

 

4. Run the App: In your terminal or command prompt, navigate to your project folder and run:

flask run

 

This will start your Flask app, and you can access it in your browser at http://localhost:5000.