Skip to content

๐Ÿ› ๏ธ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”— ๐Ÿ•น ๐Ÿ‘† ๐Ÿ‘จโ€๐ŸŽจ, ๐Ÿ–ผ โฎ๏ธ ๐ŸŽ™ ๐ŸŽ™ ๐Ÿ“Ÿ โš–๏ธ ๐Ÿ—’.

๐Ÿค™ uvicorn

๐Ÿ‘† FastAPI ๐Ÿˆธ, ๐Ÿ—„ & ๐Ÿƒ uvicorn ๐Ÿ”—:

import uvicorn
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def root():
    a = "a"
    b = "b" + a
    return {"hello world": b}


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

๐Ÿ”ƒ __name__ == "__main__"

๐Ÿ‘‘ ๐ŸŽฏ __name__ == "__main__" โœ”๏ธ ๐Ÿ“Ÿ ๐Ÿ‘ˆ ๐Ÿ› ๏ธ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ“ ๐Ÿค™ โฎ๏ธ:

$ python myapp.py

โœ‹๏ธ ๐Ÿšซ ๐Ÿค™ ๐Ÿ•โ” โž•1๏ธโƒฃ ๐Ÿ“ ๐Ÿ—„ โšซ๏ธ, ๐Ÿ’–:

from myapp import app

๐ŸŒ… โ„น

โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† ๐Ÿ“ ๐ŸŒŸ myapp.py.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿƒ โšซ๏ธ โฎ๏ธ:

$ python myapp.py

โคด๏ธ ๐Ÿ”— ๐Ÿ”ข __name__ ๐Ÿ‘† ๐Ÿ“, โœ ๐Ÿ” ๐Ÿ, ๐Ÿ”œ โœ”๏ธ ๐Ÿ’ฒ ๐ŸŽป "__main__".

, ๐Ÿ“„:

    uvicorn.run(app, host="0.0.0.0", port=8000)

๐Ÿ”œ ๐Ÿƒ.


๐Ÿ‘‰ ๐Ÿ† ๐Ÿšซ ๐Ÿ”จ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ—„ ๐Ÿ‘ˆ ๐Ÿ•น (๐Ÿ“).

, ๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ โž•1๏ธโƒฃ ๐Ÿ“ importer.py โฎ๏ธ:

from myapp import app

# Some more code

๐Ÿ‘ˆ ๐Ÿ’ผ, ๐Ÿง ๐Ÿ”ข ๐Ÿ”˜ myapp.py ๐Ÿ”œ ๐Ÿšซ โœ”๏ธ ๐Ÿ”ข __name__ โฎ๏ธ ๐Ÿ’ฒ "__main__".

, โธ:

    uvicorn.run(app, host="0.0.0.0", port=8000)

๐Ÿ”œ ๐Ÿšซ ๐Ÿ› ๏ธ.

Info

๐ŸŒ… โ„น, โœ… ๐Ÿ›‚ ๐Ÿ ๐Ÿฉบ.

๐Ÿƒ ๐Ÿ‘† ๐Ÿ“Ÿ โฎ๏ธ ๐Ÿ‘† ๐Ÿ•น

โ†ฉ๏ธ ๐Ÿ‘† ๐Ÿƒ Uvicorn ๐Ÿ’ฝ ๐Ÿ”— โšช๏ธโžก๏ธ ๐Ÿ‘† ๐Ÿ“Ÿ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿค™ ๐Ÿ‘† ๐Ÿ ๐Ÿ“‹ (๐Ÿ‘† FastAPI ๐Ÿˆธ) ๐Ÿ”— โšช๏ธโžก๏ธ ๐Ÿ•น.


๐Ÿ–ผ, ๐ŸŽ™ ๐ŸŽ™ ๐Ÿ“Ÿ, ๐Ÿ‘† ๐Ÿ’ช:

  • ๐Ÿšถ "โ„น" ๐ŸŽ›.
  • "๐Ÿšฎ ๐Ÿ“ณ...".
  • ๐Ÿ–Š "๐Ÿ"
  • ๐Ÿƒ ๐Ÿ•น โฎ๏ธ ๐ŸŽ› "Python: Current File (Integrated Terminal)".

โšซ๏ธ ๐Ÿ”œ โคด๏ธ โ–ถ๏ธ ๐Ÿ’ฝ โฎ๏ธ ๐Ÿ‘† FastAPI ๐Ÿ“Ÿ, โ›”๏ธ ๐Ÿ‘† 0๏ธโƒฃ, โ™’๏ธ.

๐Ÿ“ฅ โ” โšซ๏ธ ๐Ÿ’ช ๐Ÿ‘€:


๐Ÿšฅ ๐Ÿ‘† โš™๏ธ ๐Ÿ—’, ๐Ÿ‘† ๐Ÿ’ช:

  • ๐Ÿ“‚ "๐Ÿƒ" ๐Ÿฃ.
  • ๐Ÿ–Š ๐ŸŽ› "โ„น...".
  • โคด๏ธ ๐Ÿ”‘ ๐Ÿฃ ๐ŸŽฆ ๐Ÿ†™.
  • ๐Ÿ–Š ๐Ÿ“ โ„น (๐Ÿ‘‰ ๐Ÿ’ผ, main.py).

โšซ๏ธ ๐Ÿ”œ โคด๏ธ โ–ถ๏ธ ๐Ÿ’ฝ โฎ๏ธ ๐Ÿ‘† FastAPI ๐Ÿ“Ÿ, โ›”๏ธ ๐Ÿ‘† 0๏ธโƒฃ, โ™’๏ธ.

๐Ÿ“ฅ โ” โšซ๏ธ ๐Ÿ’ช ๐Ÿ‘€: