skip to main content

@tonyfast s notebooks

site navigation
notebook summary
title
WIP: strawberry jupyter server
description
a work in progress of a graphql schema for jupyter server.
cells
11 total
8 code
state
executed out of order
kernel
Python [conda env:root] *
language
python
name
conda-root-py
lines of code
69
outputs
2
table of contents
{"kernelspec": {"display_name": "Python [conda env:root] *", "language": "python", "name": "conda-root-py"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13"}, "title": "WIP: strawberry jupyter server", "description": "a work in progress of a graphql schema for jupyter server."}
notebook toolbar
Activate
cell ordering
1

WIP: strawberry jupyter server

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
    from jupyter_server.services.contents.largefilemanager import LargeFileManager
    from pathlib import Path
    contents = LargeFileManager(root_dir=str(Path("~").expanduser()))
4
    import strawberry as S, typing as T, strawberry.cli, enum
    from pathlib import Path
    from datetime import datetime
5
    DICT =  S.scalar(
        T.NewType("DICT", object),
        description="a dictionary",
        serialize=lambda v: v,
        parse_value=lambda v: v,
    )
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)
7
    from fastapi import FastAPI
    from strawberry.fastapi import GraphQLRouter

    graphql_app = GraphQLRouter(schema)

    app = FastAPI()
    app.include_router(graphql_app, prefix="/graphql")
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())
1 outputs.
INFO:     Shutting down
INFO:     Started server process [288580]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.

9
    I and display(IFrame("http://127.0.0.1:8000/graphql", *"100% 600".split()))
1 outputs.
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [8], line 1
----> 1 I and display(IFrame("http://127.0.0.1:8000/graphql", *"100% 600".split()))

NameError: name 'I' is not defined
10
asyncio.ensure_future(server.shutdown())
11