Skip to content

Commit

Permalink
Merge pull request #10 from Zero-True/slider-bugs
Browse files Browse the repository at this point in the history
Variables available and slider fixes
  • Loading branch information
Red-Giuliano authored Sep 14, 2023
2 parents 4db3998 + 7b67dba commit a8ded80
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
3 changes: 1 addition & 2 deletions zt_backend/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
from .response import Response
from .notebook import Notebook
from .components.slider import Slider
from .state import global_state

__all__ = ['Request', 'Response', 'Slider', 'Notebook','global_state']
__all__ = ['Request', 'Response', 'Slider', 'Notebook']
7 changes: 3 additions & 4 deletions zt_backend/models/components/slider.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from pydantic import Field, validator
from typing import Optional
from zt_backend.models.components.zt_component import ZTComponent
from zt_backend.models.validations import validate_color, validate_min_less_than_max
from zt_backend.models.state import global_state
from zt_backend.models.state import component_values

class Slider(ZTComponent):
"""A class for Slider components inheriting from ZTComponent."""
Expand All @@ -27,8 +26,8 @@ def validate_color(cls, color):
def get_value_from_global_state(cls, value, values):
id = values.get('id') # Get the id if it exists in the field values
try:
if id and id in globals()['global_state']: # Check if id exists in global_state
return globals()['global_state'][id] # Return the value associated with id in global_state
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
except Exception as e:
e
return value # If id doesn't exist in global_state, return the original value
16 changes: 13 additions & 3 deletions zt_backend/models/components/zt_component.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from pydantic import BaseModel, Field

from pydantic import BaseModel, Field, validator
from zt_backend.models.state import created_components, context_globals

class ZTComponent(BaseModel):
id: str = Field(description="Unique id for a component")
variable_name: str = Field(None, description="Optional variable name associated with a component")
variable_name: str = Field('fake', description="Optional variable name associated with a component")

@validator('id', always=True)
def validate_unique_component_id(cls, id):
if context_globals['exec_mode'] and id in created_components:
raise Exception("Component ID is not unique")
elif context_globals['exec_mode']:
created_components.append(id)
return id
else:
return id
4 changes: 3 additions & 1 deletion zt_backend/models/state.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
global_state = {}
component_values = {}
created_components=[]
context_globals={"exec_mode": False}
1 change: 1 addition & 0 deletions zt_backend/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fastapi.responses import FileResponse
from zt_backend.models import request, notebook, response
from zt_backend.runner.execute_code import execute_request
from zt_backend.models.state import component_values, created_components, context_globals
import os
import uuid
import toml
Expand Down
44 changes: 22 additions & 22 deletions zt_backend/runner/execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
from contextlib import redirect_stdout
from zt_backend.models import request, response
from zt_backend.models.components.zt_component import ZTComponent
from zt_backend.models.state import global_state


from zt_backend.models.state import component_values, created_components, context_globals

def execute_request(request: request.Request):
context_globals['exec_mode'] = True
cell_outputs = []
global_state.update(request.components)
component_globals={'global_state': global_state}

component_values.update(request.components)
component_globals={'global_state': component_values}
added_components = []
for code_cell in request.cells:
cell_outputs.append(execute_code(code_cell, component_globals))
return response.Response(cells=cell_outputs)

def execute_code(code_cell: request.CodeRequest, component_globals):
f = StringIO()
with redirect_stdout(f):
exec(code_cell.code, component_globals)
global_state.clear()
response_components = []
for component_name, value in component_globals.items():
if isinstance(value, ZTComponent):
value.variable_name = component_name
response_components.append(value)
return response.CellResponse(id=code_cell.id, components=response_components, output=f.getvalue())


f = StringIO()
with redirect_stdout(f):
try:
exec(code_cell.code, component_globals)
except Exception as e:
print(e)
response_components = []
for component_name, value in component_globals.items():
if isinstance(value, ZTComponent) and value.id not in added_components:
value.variable_name = component_name
added_components.append(value.id)
response_components.append(value)
cell_outputs.append(response.CellResponse(id=code_cell.id, components=response_components, output=f.getvalue()))
component_values.clear()
created_components.clear()
context_globals['exec_mode'] = False
return response.Response(cells=cell_outputs)

0 comments on commit a8ded80

Please sign in to comment.