Skip to content

Instantly share code, notes, and snippets.

@jugmac00
Last active November 7, 2023 19:37
Show Gist options
  • Save jugmac00/522f04a6b4588090cbb70a01402c489b to your computer and use it in GitHub Desktop.
Save jugmac00/522f04a6b4588090cbb70a01402c489b to your computer and use it in GitHub Desktop.
Anleitung: Installation von FastAPI unter Windows
"""
Python 3.8 installieren ( + Pfad aktivieren)
In der Konsole:
# legt virtual env an
Python -m venv fastapi
cd fastapi
# virtualenv aktivieren
irgendwas mit Scripts/activate
# installiert das Programm
pip install fastapi uvicorn
# startet den Server
uvicorn main:app --reload
# Achtung - das Terminal hängt anscheinend - Terminal markieren + Enter => dann lädt die Anwendung
Datei bitte speichern als main.py
Unser Code enthält noch keine Beispieldaten!
Es müssen vorher Daten z.B. mittels curl oder der interaktiven Demo generiert werden:
http://127.0.0.1:8000/docs
**********************************************************************************************
"""
from fastapi import FastAPI
from pydantic import BaseModel
BASKET = {}
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: bool = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/")
def read_item():
return BASKET
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
#return {"item_id": item_id, "q": q}
return BASKET[item_id]
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
BASKET[item_id] = item
return {"item_name": item.name, "item_id": item_id}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment