From 7b67ea7e663837b31660207d032e131b0cbd97fd Mon Sep 17 00:00:00 2001 From: Carson-Shaar Date: Mon, 18 Sep 2023 16:33:51 -0400 Subject: [PATCH 1/3] Slider runs, Delete Cell, Origin Id in run code --- zt_backend/models/__init__.py | 4 +-- zt_backend/models/components/slider.py | 2 +- zt_backend/models/components/zt_component.py | 2 +- zt_backend/models/generate_schema.py | 3 ++- zt_backend/models/request.py | 25 +++++------------ zt_backend/router.py | 12 ++++++++- zt_frontend/src/App.vue | 24 +++++++++++++---- zt_frontend/src/components/CodeComponent.vue | 23 +++++++++------- zt_frontend/src/types/component_request.d.ts | 15 +++++++++++ zt_frontend/src/types/delete_request.d.ts | 13 +++++++++ zt_frontend/src/types/request.d.ts | 2 ++ zt_schema/component_request.json | 28 ++++++++++++++++++++ zt_schema/delete_request.json | 13 +++++++++ zt_schema/notebook.json | 2 +- zt_schema/request.json | 5 ++++ zt_schema/response.json | 2 +- zt_schema/slider.json | 2 +- 17 files changed, 135 insertions(+), 42 deletions(-) create mode 100644 zt_frontend/src/types/component_request.d.ts create mode 100644 zt_frontend/src/types/delete_request.d.ts create mode 100644 zt_schema/component_request.json create mode 100644 zt_schema/delete_request.json diff --git a/zt_backend/models/__init__.py b/zt_backend/models/__init__.py index 7bedb9e8..9279bab7 100644 --- a/zt_backend/models/__init__.py +++ b/zt_backend/models/__init__.py @@ -1,6 +1,6 @@ -from .request import Request +from .request import Request, ComponentRequest, DeleteRequest from .response import Response from .notebook import Notebook from .components.slider import Slider -__all__ = ['Request', 'Response', 'Slider', 'Notebook'] \ No newline at end of file +__all__ = ['Request', 'Response', 'Slider', 'Notebook', 'ComponentRequest', 'DeleteRequest'] \ No newline at end of file diff --git a/zt_backend/models/components/slider.py b/zt_backend/models/components/slider.py index a380317a..2d03726d 100644 --- a/zt_backend/models/components/slider.py +++ b/zt_backend/models/components/slider.py @@ -24,7 +24,7 @@ def validate_color(cls, color): @field_validator('value') def get_value_from_global_state(cls, value, values): - id = values.get('id') # Get the id if it exists in the field values + id = values.data['id'] # Get the id if it exists in the field values try: if id and id in component_values: # Check if id exists in global_state return component_values[id] # Return the value associated with id in global_state diff --git a/zt_backend/models/components/zt_component.py b/zt_backend/models/components/zt_component.py index 90db5bc1..84b213d6 100644 --- a/zt_backend/models/components/zt_component.py +++ b/zt_backend/models/components/zt_component.py @@ -5,7 +5,7 @@ class ZTComponent(BaseModel): id: str = Field(description="Unique id for a component") variable_name: str = Field('fake', description="Optional variable name associated with a component") - @field_validator('id',mode='before') + @field_validator('id', mode='before') def validate_unique_component_id(cls, id): if context_globals['exec_mode'] and id in created_components: raise Exception("Component ID is not unique") diff --git a/zt_backend/models/generate_schema.py b/zt_backend/models/generate_schema.py index 190fb70a..e32c6683 100644 --- a/zt_backend/models/generate_schema.py +++ b/zt_backend/models/generate_schema.py @@ -1,5 +1,4 @@ from zt_backend.models import * -from fastapi.encoders import jsonable_encoder import json def generate_json(model, name): @@ -8,6 +7,8 @@ def generate_json(model, name): def generate_schema(): generate_json(Request, 'request') + generate_json(ComponentRequest, 'component_request') + generate_json(DeleteRequest, 'delete_request') generate_json(Response, 'response') generate_json(Slider, 'slider') generate_json(Notebook, 'notebook') diff --git a/zt_backend/models/request.py b/zt_backend/models/request.py index 17dfb2dc..461ce12b 100644 --- a/zt_backend/models/request.py +++ b/zt_backend/models/request.py @@ -1,28 +1,15 @@ from pydantic import BaseModel from typing import List, Dict, Union -from zt_backend.models.components.slider import Slider -from zt_backend.models.components.zt_component import ZTComponent - -# def deserialize_component(data: Dict[str, Any]) -> ZTComponent: -# component_map = { -# "v-slider": Slider, -# # add other component types here -# } -# component_class = data.get("component") -# if component_class not in component_map: -# raise ValueError(f"Invalid component class: {component_class}") -# return component_map[component_class].model_validate(data) class CodeRequest(BaseModel): id: str code: str - class Request(BaseModel): + originId: str cells: List[CodeRequest] components: Dict[str, Union[str, bool, int]] - class Cell(BaseModel): code: str defined_names: List[str] @@ -34,9 +21,9 @@ class Cell(BaseModel): class CodeDict(BaseModel): cells: Dict[str, Cell] +class ComponentRequest(BaseModel): + componentId: str + componentValue: Union[str, bool, int] - # @root_validator(pre=True) - # def deserialize_components(cls, values): - # components = values.get('components', []) - # values['components'] = [deserialize_component(comp) for comp in components] - # return values \ No newline at end of file +class DeleteRequest(BaseModel): + cellId: str diff --git a/zt_backend/router.py b/zt_backend/router.py index bb16bd02..5e6becc5 100644 --- a/zt_backend/router.py +++ b/zt_backend/router.py @@ -29,6 +29,10 @@ def runcode(request: request.Request): globalStateUpdate(run_response=response) return response +@router.post("/api/component_run") +def runcode(request: request.ComponentRequest): + print(request) + @router.post("/api/create_cell") def create_cell(): createdCell = notebook.CodeCell( @@ -41,6 +45,10 @@ def create_cell(): globalStateUpdate(newCell=createdCell) return createdCell +@router.post("/api/delete_cell") +def delete_cell(deleteRequest: request.DeleteRequest): + globalStateUpdate(deletedCell=deleteRequest.cellId) + @router.get("/api/notebook") def get_notebook(): return get_notebook() @@ -50,10 +58,12 @@ def get_notebook(): notebook_data = toml.load(project_file) return notebook.Notebook(**notebook_data) -def globalStateUpdate(newCell: notebook.CodeCell=None, run_request: request.Request=None, run_response: response.Response=None): +def globalStateUpdate(newCell: notebook.CodeCell=None, deletedCell: str=None, run_request: request.Request=None, run_response: response.Response=None): zt_notebook = get_notebook() if newCell is not None: zt_notebook.cells[newCell.id] = newCell + if deletedCell is not None: + del zt_notebook.cells[deletedCell] if run_request is not None: for requestCell in run_request.cells: zt_notebook.cells[requestCell.id].code = requestCell.code diff --git a/zt_frontend/src/App.vue b/zt_frontend/src/App.vue index 4f7be86f..4ebf161c 100644 --- a/zt_frontend/src/App.vue +++ b/zt_frontend/src/App.vue @@ -10,11 +10,13 @@ + @runCode="runCode" + @componentValueChange="componentValueChange" + @deleteCell="deleteCell"/> Add Code Cell - + Add Markdown Cell @@ -25,6 +27,8 @@