WIP: strawberry jupyter serverยค
a work in progress of a graphql
schema for jupyter server.
https://strawberry.rocks/docs/general/schema-basics
# %pip install 'strawberry-graphql[debug-server]'
from jupyter_server.services.contents.largefilemanager import LargeFileManager
from pathlib import Path
contents = LargeFileManager(root_dir=str(Path("~").expanduser()))
import strawberry as S, typing as T, strawberry.cli, enum
from pathlib import Path
from datetime import datetime
DICT = S.scalar(
T.NewType("DICT", object),
description="a dictionary",
serialize=lambda v: v,
parse_value=lambda v: v,
)
@strawberry.enum
class ContentType(enum.Enum):
FILE = "file"
NOTEBOOK = "notebook"
DIRECTORY = "directory"
@S.type
class Base:
name: str
path: str
last_modified: datetime
created: datetime
format: str
mimetype: T.Optional[str]
size: T.Optional[int]
writable: bool
type: ContentType
Cells = T.Union[DICT]
@S.type
class NbFormat:
cells: list[Cells]
metadata: DICT
@S.type
class Notebook(Base):
content: DICT
@S.type
class File(Base):
content: str
@S.type
class Directory(Base):
content: T.Annotated["Kinds", "__main__"]
Kinds = T.Union[Notebook, File, Directory]
@S.type
class Query:
@S.field
def search(self, dir: str = "", kind: T.Optional[ContentType] = None) -> Kinds:
return Directory(**contents.get(dir, ))
schema = strawberry.Schema(Query)
from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter
graphql_app = GraphQLRouter(schema)
app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")
if (I := "__file__" not in locals()):
from IPython.display import IFrame
import uvicorn, asyncio
__import__("nest_asyncio").apply()
config = uvicorn.Config(app)
if "server" in locals():
asyncio.ensure_future(server.shutdown())
server = uvicorn.Server(config=config)
asyncio.ensure_future(server.serve())
I and display(IFrame("http://127.0.0.1:8000/graphql", *"100% 600".split()))
asyncio.ensure_future(server.shutdown())