nbconvert / app.py
julien-c's picture
julien-c HF staff
hook httpx
136ae30 verified
raw
history blame
1.09 kB
import os
import httpx
from nbconvert import HTMLExporter
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.responses import FileResponse, JSONResponse, HTMLResponse
from starlette.requests import Request
from starlette.routing import Route
client = httpx.AsyncClient()
html_exporter = HTMLExporter(template_name="classic")
async def homepage(_):
return FileResponse("static/index.html")
async def healthz(_):
return JSONResponse({"success": True})
async def convert(req: Request):
url = req.query_params.get("url")
if not url:
raise HTTPException(400, "Param url is missing")
r = await client.get(
url,
headers={"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"},
follow_redirects=True,
# httpx no follow redirect by default
)
print(r.text)
return HTMLResponse("<strong>FOO</strong>")
app = Starlette(
debug=False,
routes=[
Route("/", homepage),
Route("/healthz", healthz),
Route("/convert", convert),
],
)