darabos commited on
Commit
e0b4f7e
·
1 Parent(s): 1a56106

Send graph to backend.

Browse files
Files changed (4) hide show
  1. main.py +34 -1
  2. requirements.txt +1 -0
  3. web/src/App.vue +17 -0
  4. web/vite.config.ts +3 -0
main.py CHANGED
@@ -1,6 +1,33 @@
1
  from typing import Union
2
-
3
  import fastapi
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  app = fastapi.FastAPI()
6
 
@@ -13,3 +40,9 @@ def read_root():
13
  @app.get("/items/{item_id}")
14
  def read_item(item_id: int, q: Union[str, None] = None):
15
  return {"item_id": item_id, "q": q}
 
 
 
 
 
 
 
1
  from typing import Union
 
2
  import fastapi
3
+ import pydantic
4
+
5
+ class Position(pydantic.BaseModel):
6
+ x: float
7
+ y: float
8
+
9
+ class WorkspaceNode(pydantic.BaseModel):
10
+ id: str
11
+ title: str
12
+ type: str
13
+ position: Position
14
+
15
+ class WorkspaceConnection(pydantic.BaseModel):
16
+ id: str
17
+ # Baklava.js calls it "from", but that's a reserved keyword in Python.
18
+ src: str = pydantic.Field(None, alias='from')
19
+ dst: str = pydantic.Field(None, alias='to')
20
+
21
+ class WorkspaceGraph(pydantic.BaseModel):
22
+ nodes: list[WorkspaceNode]
23
+ connections: list[WorkspaceConnection]
24
+ panning: Position
25
+ scaling: float
26
+ nodes: list[WorkspaceNode]
27
+
28
+ class Workspace(pydantic.BaseModel):
29
+ graph: WorkspaceGraph
30
+
31
 
32
  app = fastapi.FastAPI()
33
 
 
40
  @app.get("/items/{item_id}")
41
  def read_item(item_id: int, q: Union[str, None] = None):
42
  return {"item_id": item_id, "q": q}
43
+
44
+
45
+ @app.post("/api/save")
46
+ def save(ws: Workspace):
47
+ print(ws)
48
+ return {"status": "ok"}
requirements.txt CHANGED
@@ -2,3 +2,4 @@ fastapi
2
  networkx
3
  numpy
4
  pandas
 
 
2
  networkx
3
  numpy
4
  pandas
5
+ uvicorn
web/src/App.vue CHANGED
@@ -79,4 +79,21 @@ engine.events.afterRun.subscribe(token, (result) => {
79
  applyResult(result, baklava.editor);
80
  engine.resume();
81
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  </script>
 
79
  applyResult(result, baklava.editor);
80
  engine.resume();
81
  });
82
+ let lastSave;
83
+ baklava.editor.nodeEvents.update.subscribe(token, async (result) => {
84
+ const s = JSON.stringify(baklava.editor.save());
85
+ if (s !== lastSave) {
86
+ lastSave = s;
87
+ console.log('save', JSON.parse(s));
88
+ const res = await fetch('/api/save', {
89
+ method: 'POST',
90
+ headers: {
91
+ 'Content-Type': 'application/json',
92
+ },
93
+ body: s,
94
+ });
95
+ const j = await res.json();
96
+ console.log('save response', j);
97
+ }
98
+ });
99
  </script>
web/vite.config.ts CHANGED
@@ -4,4 +4,7 @@ import vue from '@vitejs/plugin-vue'
4
  // https://vitejs.dev/config/
5
  export default defineConfig({
6
  plugins: [vue()],
 
 
 
7
  })
 
4
  // https://vitejs.dev/config/
5
  export default defineConfig({
6
  plugins: [vue()],
7
+ server: {
8
+ proxy: { '/api': 'http://127.0.0.1:8000' },
9
+ },
10
  })