Spaces:
Running
Running
Fix ty warnings in lynxkite-app and examples.
Browse files
examples/fake_data.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from lynxkite.core.ops import op
|
| 2 |
-
from faker import Faker
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
faker = Faker()
|
|
|
|
| 1 |
from lynxkite.core.ops import op
|
| 2 |
+
from faker import Faker # ty: ignore[unresolved-import]
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
faker = Faker()
|
examples/word2vec.py
CHANGED
|
@@ -6,7 +6,7 @@ ENV = "LynxKite Graph Analytics"
|
|
| 6 |
|
| 7 |
@op(ENV, "Word2vec for the top 1000 words", slow=True)
|
| 8 |
def word2vec_1000():
|
| 9 |
-
import staticvectors
|
| 10 |
|
| 11 |
model = staticvectors.StaticVectors("neuml/word2vec-quantized")
|
| 12 |
df = pd.read_csv(
|
|
|
|
| 6 |
|
| 7 |
@op(ENV, "Word2vec for the top 1000 words", slow=True)
|
| 8 |
def word2vec_1000():
|
| 9 |
+
import staticvectors # ty: ignore[unresolved-import]
|
| 10 |
|
| 11 |
model = staticvectors.StaticVectors("neuml/word2vec-quantized")
|
| 12 |
df = pd.read_csv(
|
lynxkite-app/src/lynxkite_app/crdt.py
CHANGED
|
@@ -7,8 +7,8 @@ import pathlib
|
|
| 7 |
import fastapi
|
| 8 |
import os.path
|
| 9 |
import pycrdt.websocket
|
| 10 |
-
import pycrdt.store
|
| 11 |
-
import uvicorn
|
| 12 |
import builtins
|
| 13 |
from lynxkite.core import workspace, ops
|
| 14 |
|
|
@@ -59,7 +59,8 @@ class WorkspaceWebsocketServer(pycrdt.websocket.WebsocketServer):
|
|
| 59 |
room = pycrdt.websocket.YRoom(
|
| 60 |
ystore=ystore, ydoc=ydoc, exception_handler=ws_exception_handler
|
| 61 |
)
|
| 62 |
-
room
|
|
|
|
| 63 |
|
| 64 |
def on_change(changes):
|
| 65 |
task = asyncio.create_task(workspace_changed(name, changes, ws))
|
|
@@ -106,7 +107,8 @@ class CodeWebsocketServer(WorkspaceWebsocketServer):
|
|
| 106 |
room = pycrdt.websocket.YRoom(
|
| 107 |
ystore=ystore, ydoc=ydoc, exception_handler=ws_exception_handler
|
| 108 |
)
|
| 109 |
-
room
|
|
|
|
| 110 |
|
| 111 |
def on_change(changes):
|
| 112 |
asyncio.create_task(code_changed(name, changes, text))
|
|
@@ -163,6 +165,7 @@ def crdt_update(
|
|
| 163 |
ValueError: If the Python object provided is not a dict or list.
|
| 164 |
"""
|
| 165 |
if isinstance(python_obj, dict):
|
|
|
|
| 166 |
for key, value in python_obj.items():
|
| 167 |
if key in non_collaborative_fields:
|
| 168 |
crdt_obj[key] = value
|
|
@@ -179,6 +182,7 @@ def crdt_update(
|
|
| 179 |
else:
|
| 180 |
crdt_obj[key] = value
|
| 181 |
elif isinstance(python_obj, list):
|
|
|
|
| 182 |
for i, value in enumerate(python_obj):
|
| 183 |
if isinstance(value, dict):
|
| 184 |
if i >= len(crdt_obj):
|
|
@@ -220,7 +224,7 @@ last_known_versions = {}
|
|
| 220 |
delayed_executions = {}
|
| 221 |
|
| 222 |
|
| 223 |
-
async def workspace_changed(name: str, changes: pycrdt.MapEvent, ws_crdt: pycrdt.Map):
|
| 224 |
"""Callback to react to changes in the workspace.
|
| 225 |
|
| 226 |
Args:
|
|
@@ -295,6 +299,14 @@ async def code_changed(name: str, changes: pycrdt.TextEvent, text: pycrdt.Text):
|
|
| 295 |
f.write(contents)
|
| 296 |
|
| 297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
@contextlib.asynccontextmanager
|
| 299 |
async def lifespan(app):
|
| 300 |
global ws_websocket_server
|
|
|
|
| 7 |
import fastapi
|
| 8 |
import os.path
|
| 9 |
import pycrdt.websocket
|
| 10 |
+
import pycrdt.store.file
|
| 11 |
+
import uvicorn.protocols.utils
|
| 12 |
import builtins
|
| 13 |
from lynxkite.core import workspace, ops
|
| 14 |
|
|
|
|
| 59 |
room = pycrdt.websocket.YRoom(
|
| 60 |
ystore=ystore, ydoc=ydoc, exception_handler=ws_exception_handler
|
| 61 |
)
|
| 62 |
+
# We hang the YDoc pointer on the room, so it only gets garbage collected when the room does.
|
| 63 |
+
room.ws = ws # ty: ignore[unresolved-attribute]
|
| 64 |
|
| 65 |
def on_change(changes):
|
| 66 |
task = asyncio.create_task(workspace_changed(name, changes, ws))
|
|
|
|
| 107 |
room = pycrdt.websocket.YRoom(
|
| 108 |
ystore=ystore, ydoc=ydoc, exception_handler=ws_exception_handler
|
| 109 |
)
|
| 110 |
+
# We hang the YDoc pointer on the room, so it only gets garbage collected when the room does.
|
| 111 |
+
room.text = text # ty: ignore[unresolved-attribute]
|
| 112 |
|
| 113 |
def on_change(changes):
|
| 114 |
asyncio.create_task(code_changed(name, changes, text))
|
|
|
|
| 165 |
ValueError: If the Python object provided is not a dict or list.
|
| 166 |
"""
|
| 167 |
if isinstance(python_obj, dict):
|
| 168 |
+
assert isinstance(crdt_obj, pycrdt.Map), "CRDT object must be a Map for a dict input"
|
| 169 |
for key, value in python_obj.items():
|
| 170 |
if key in non_collaborative_fields:
|
| 171 |
crdt_obj[key] = value
|
|
|
|
| 182 |
else:
|
| 183 |
crdt_obj[key] = value
|
| 184 |
elif isinstance(python_obj, list):
|
| 185 |
+
assert isinstance(crdt_obj, pycrdt.Array), "CRDT object must be an Array for a list input"
|
| 186 |
for i, value in enumerate(python_obj):
|
| 187 |
if isinstance(value, dict):
|
| 188 |
if i >= len(crdt_obj):
|
|
|
|
| 224 |
delayed_executions = {}
|
| 225 |
|
| 226 |
|
| 227 |
+
async def workspace_changed(name: str, changes: list[pycrdt.MapEvent], ws_crdt: pycrdt.Map):
|
| 228 |
"""Callback to react to changes in the workspace.
|
| 229 |
|
| 230 |
Args:
|
|
|
|
| 299 |
f.write(contents)
|
| 300 |
|
| 301 |
|
| 302 |
+
ws_websocket_server: WorkspaceWebsocketServer
|
| 303 |
+
code_websocket_server: CodeWebsocketServer
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
def get_room(name):
|
| 307 |
+
return ws_websocket_server.get_room(name)
|
| 308 |
+
|
| 309 |
+
|
| 310 |
@contextlib.asynccontextmanager
|
| 311 |
async def lifespan(app):
|
| 312 |
global ws_websocket_server
|
lynxkite-app/src/lynxkite_app/main.py
CHANGED
|
@@ -8,7 +8,7 @@ import pathlib
|
|
| 8 |
import pkgutil
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
| 10 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 11 |
-
import starlette
|
| 12 |
from lynxkite.core import ops
|
| 13 |
from lynxkite.core import workspace
|
| 14 |
from . import crdt
|
|
@@ -136,7 +136,7 @@ async def upload(req: fastapi.Request):
|
|
| 136 |
@app.post("/api/execute_workspace")
|
| 137 |
async def execute_workspace(name: str):
|
| 138 |
"""Trigger and await the execution of a workspace."""
|
| 139 |
-
room = await crdt.
|
| 140 |
ws_pyd = workspace.Workspace.model_validate(room.ws.to_py())
|
| 141 |
await crdt.execute(name, room.ws, ws_pyd)
|
| 142 |
|
|
|
|
| 8 |
import pkgutil
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
| 10 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 11 |
+
import starlette.exceptions
|
| 12 |
from lynxkite.core import ops
|
| 13 |
from lynxkite.core import workspace
|
| 14 |
from . import crdt
|
|
|
|
| 136 |
@app.post("/api/execute_workspace")
|
| 137 |
async def execute_workspace(name: str):
|
| 138 |
"""Trigger and await the execution of a workspace."""
|
| 139 |
+
room = await crdt.get_room(name)
|
| 140 |
ws_pyd = workspace.Workspace.model_validate(room.ws.to_py())
|
| 141 |
await crdt.execute(name, room.ws, ws_pyd)
|
| 142 |
|
lynxkite-core/src/lynxkite/core/workspace.py
CHANGED
|
@@ -49,6 +49,13 @@ class WorkspaceNodeData(BaseConfig):
|
|
| 49 |
data["op_id"] = data["title"]
|
| 50 |
return data
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
class WorkspaceNode(BaseConfig):
|
| 54 |
# Most of these fields are shared with ReactFlow.
|
|
|
|
| 49 |
data["op_id"] = data["title"]
|
| 50 |
return data
|
| 51 |
|
| 52 |
+
@pydantic.model_validator(mode="before")
|
| 53 |
+
@classmethod
|
| 54 |
+
def ignore_meta(cls, data: dict) -> dict:
|
| 55 |
+
"""Metadata is never loaded. We will use fresh metadata."""
|
| 56 |
+
data["meta"] = None
|
| 57 |
+
return data
|
| 58 |
+
|
| 59 |
|
| 60 |
class WorkspaceNode(BaseConfig):
|
| 61 |
# Most of these fields are shared with ReactFlow.
|