Skip to content

Instantly share code, notes, and snippets.

@diegolirio
Last active January 5, 2024 20:47
Show Gist options
  • Save diegolirio/d5e6cd00a8fb2607db9ce51006b6ad73 to your computer and use it in GitHub Desktop.
Save diegolirio/d5e6cd00a8fb2607db9ce51006b6ad73 to your computer and use it in GitHub Desktop.
Python + Fast API

Python + Fast API

Tecnologias utilizadas:

  • Python3
  • Pip
  • Pipenv
  • Fast API
  • Uvicorn

Instalando Python 3

Pyenv

Semelhante ao sdkman

# for Mac
brew install pyenv
# for Linux
sudo apt-get update

sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm gettext libncurses5-dev tk-dev tcl-dev blt-dev libgdbm-dev git python2-dev python3-dev aria2 lzma liblzma-dev
pyenv --version

pyenv install 3.11.7

pyenv versions
pyenv uninstall 3.11.7
pyenv global 3.11.7

Ou Ubuntu

No Ubuntu e sistemas baseados no Debian, você pode usar o seguinte comando para instalar o Python 3:

sudo apt update
sudo apt install python3

Ou Macbook

brew install python
python3 --version

Obs: Caso nao tenho o Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Instalando o PIP

Gerenciador de pacotes para Python. Ele é usado para instalar e gerenciar bibliotecas e módulos Python de forma fácil

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
pip3 --version

Pipenv

Pipenv é uma ferramenta de gerenciamento de ambientes virtuais e de dependências para Python com o arquivo Pipfile

pip3 install pipenv

Instalar o FastAPI e Uvicorn

FastAPI é um Framework web moderno para construção de APIs. Uvicorn é um servidor ASGI (Asynchronous Server Gateway Interface) para Python. Ele é frequentemente usado para executar aplicativos web assíncronos, incluindo aqueles construídos com o FastAPI.

pipenv --python 3.11.7
pipenv install fastapi uvicorn

main.py

# main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, query_param: str = None):
    return {"item_id": item_id, "query_param": query_param}

Run

Execute no Terminal

pipenv run uvicorn main:app --reload

Isso iniciará o servidor Uvicorn com o seu aplicativo FastAPI. O parâmetro --reload é opcional e permite que o servidor seja reiniciado automaticamente quando o código é alterado.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment