29
loading...
This website collects cookies to deliver better user experience
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def home():
return {'data':'Get This And Be Happy'}
FastAPI
from fastapi
from fastapi import FastAPI
FastAPI
Is The Main Class You Will Use To Start With The APIapp
Of The Class FastAPI
So We Can Use All The Functions And Methods Provided By This Class.@app.get('/)
, If You Don't Know What The @
Symbol Is, Let Me Tell You (If You Know What Is it Please Skip To "Explaining An Endpoint")Decorators
In Python(A Decorator allows a user to add new functionality to an existing object without modifying its structure.)foo
def foo(f):
print('doing')
f()
print('done')
func
@foo
def func():
print('inside foo')
func
Will Be Give To Function foo
As A Parameter, So By Now You Know What Will Be The Result,doing
inside foo
done
/some_address
(slash some address) Where Clients Can Come And Do Stuff Like We Have google.com/home
, Here We Defined Our Endpoint As /
. This Means Who Ever Comes To http://127.0.0.1:8000
(or whatever domain name our api is running), That Client Is Recived Where We Defined /
./
and may return something, Similarly If We Create /hello
, The Function Defined Below Will Be Executed If Someone Visits /hello
.{'data':'I AM JSON'}
. Get The Point Now? FastAPI Converts A Dict
object to a JSON
Object.(See here or here to read more about JSON)