Spaces:
Running
Running
File size: 1,242 Bytes
1a56106 e0b4f7e a3bb72f e0b4f7e af53b62 e0b4f7e af53b62 e0b4f7e af53b62 e0b4f7e af53b62 e0b4f7e af53b62 e0b4f7e 1a56106 e0b4f7e af53b62 3d534f4 a3bb72f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
from typing import Union
import fastapi
import pydantic
import networkx as nx
class Position(pydantic.BaseModel):
x: float
y: float
class WorkspaceNodeData(pydantic.BaseModel):
title: str
params: dict
class WorkspaceNode(pydantic.BaseModel):
id: str
type: str
data: WorkspaceNodeData
position: Position
class WorkspaceEdge(pydantic.BaseModel):
id: str
source: str
target: str
class Workspace(pydantic.BaseModel):
nodes: list[WorkspaceNode]
edges: list[WorkspaceEdge]
app = fastapi.FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
@app.post("/api/save")
def save(ws: Workspace):
print(ws)
G = nx.scale_free_graph(4)
return {'graph':{
'attributes': {
'name': 'My Graph'
},
'options': {
'allowSelfLoops': True,
'multi': False,
'type': 'mixed'
},
'nodes': [
{'key': 'Thomas'},
{'key': 'Eric'}
],
'edges': [
{
'key': 'T->E',
'source': 'Thomas',
'target': 'Eric',
}
]
}}
return {"graph": list(nx.to_edgelist(G))}
|