index
|
execution_count
|
cell_type
|
toolbar
|
started_at
|
completed_at
|
source
|
loc
|
metadata
|
outputs
|
1
|
|
|
|
|
|
# WIP: strawberry jupyter server
a work in progress of a `graphql` schema for jupyter server.
|
|
3
|
a work in progress of a
graphql
schema for jupyter server.
|
2
|
|
|
|
|
|
https://strawberry.rocks/docs/general/schema-basics
# %pip install 'strawberry-graphql[debug-server]'
|
|
3
|
https://strawberry.rocks/docs/general/schema-basics
# %pip install 'strawberry-graphql[debug-server]'
|
3
|
|
|
|
|
|
from jupyter_server.services.contents.largefilemanager import LargeFileManager
from pathlib import Path
contents = LargeFileManager(root_dir=str(Path("~").expanduser()))
|
|
3
|
0 outputs.
|
4
|
|
|
|
|
|
import strawberry as S, typing as T, strawberry.cli, enum
from pathlib import Path
from datetime import datetime
|
|
3
|
0 outputs.
|
5
|
|
|
|
|
|
DICT = S.scalar(
T.NewType("DICT", object),
description="a dictionary",
serialize=lambda v: v,
parse_value=lambda v: v,
)
|
|
6
|
0 outputs.
|
6
|
|
|
|
|
|
@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)
|
|
47
|
0 outputs.
|
7
|
|
|
|
|
|
from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter
graphql_app = GraphQLRouter(schema)
app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")
|
|
7
|
0 outputs.
|
8
|
|
|
|
|
|
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())
|
|
11
|
1 outputs.
|
9
|
|
|
|
|
|
I and display(IFrame("http://127.0.0.1:8000/graphql", *"100% 600".split()))
|
|
1
|
1 outputs.
|
10
|
|
|
|
|
|
asyncio.ensure_future(server.shutdown())
|
|
1
|
asyncio.ensure_future(server.shutdown())
|
11
|
|
|
|
|
|
|
|
0
|
|