Web app
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Body, Depends, FastAPI, HTTPException, Path, Query
|
||||
|
||||
from ipython_demo import App
|
||||
from ipython_demo.models import CallOptions
|
||||
|
||||
app = FastAPI(title="IPython Demo", version="0.1.0")
|
||||
|
||||
ShellApp = App() # global, scary
|
||||
|
||||
|
||||
@app.get("/shells")
|
||||
def list_shells():
|
||||
return ShellApp.list_shells()
|
||||
|
||||
|
||||
@app.post("/shells/new")
|
||||
def create_shell():
|
||||
return ShellApp.create_shell()
|
||||
|
||||
|
||||
@app.get("/shells/last")
|
||||
def get_last_shell():
|
||||
try:
|
||||
return ShellApp.get_last_shell()
|
||||
except KeyError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||
|
||||
|
||||
@app.get("/shells/{shell_name}")
|
||||
def get_shell(shell_name: str):
|
||||
try:
|
||||
return ShellApp.get_shell(shell_name)
|
||||
except KeyError as error:
|
||||
raise HTTPException(status_code=404, detail=str(error)) from error
|
||||
|
||||
|
||||
@app.post("/shells/{shell_name}/run")
|
||||
def run_code(
|
||||
shell_name: Annotated[str, Path(..., description="The shell to run code in")],
|
||||
code: Annotated[str, Body(..., description="The code to execute", media_type="text/plain")],
|
||||
options: Annotated[CallOptions, Depends()],
|
||||
):
|
||||
return ShellApp.run_code(code, shell_name, options=options)
|
||||
|
||||
|
||||
@app.post("/run")
|
||||
def run_code_2(
|
||||
code: Annotated[str, Body(..., description="The code to execute", media_type="text/plain")],
|
||||
options: Annotated[CallOptions, Depends()],
|
||||
shell_name: Annotated[str, Query(description="The shell to run code in")] = "last",
|
||||
):
|
||||
return ShellApp.run_code(code, shell_name, options=options)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="::", port=8000, log_level="info")
|
||||
Reference in New Issue
Block a user