Skip to content

Commit

Permalink
Issues resolve (#30)
Browse files Browse the repository at this point in the history
* Fix markdown+text parsing

* temp schema

* types refresh

* tests

* GHA fix

* typescript fix

* comment

* 'revert'
  • Loading branch information
Carson-Shaar authored Sep 21, 2023
1 parent 483130a commit 72310c1
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 547 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 .components.zt_component import ZTComponent

__all__ = ['Request', 'Response', 'Slider', 'Notebook', 'ComponentRequest', 'DeleteRequest','ZTComponent','CreateRequest']
__all__ = ['Request', 'Response', 'Slider', 'Notebook', 'ComponentRequest', 'DeleteRequest','CreateRequest']
1 change: 1 addition & 0 deletions zt_backend/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class CodeRequest(BaseModel):
id: str
code: str
cellType: str = Field(enum=['code', 'markdown', 'text'])

class Request(BaseModel):
originId: str
Expand Down
2 changes: 1 addition & 1 deletion zt_backend/runner/code_cell_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_loaded_names(module, defined_names) -> List[str]:

def parse_cells(request: Request) -> CodeDict:
cell_dict = {}
for cell in request.cells:
for cell in [c for c in request.cells if c.cellType=='code']:
module = astroid.parse(cell.code)
function_names, function_arguments = get_functions(module)
defined_names = get_defined_names(module) + get_imports(module) + function_names
Expand Down
58 changes: 29 additions & 29 deletions zt_backend/tests/test_code_cell_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

# Test 1: No dependencies between cells
def test_no_dependencies():
cells = Request(originId="0", cells=[CodeRequest(id="0",code="a = 1"),
CodeRequest(id="1",code="b = 2"),
CodeRequest(id="2",code="c = 3")],\
cells = Request(originId="0", cells=[CodeRequest(id="0",code="a = 1", cellType='code'),
CodeRequest(id="1",code="b = 2", cellType='code'),
CodeRequest(id="2",code="c = 3", cellType='code')],\
components={"":""})
cell_dict = build_dependency_graph(parse_cells(cells))
assert cell_dict.cells['0'].defined_names == ['a'] and cell_dict.cells['0'].loaded_names == [], "Test 1 Failed"
Expand All @@ -14,9 +14,9 @@ def test_no_dependencies():

# Test 2: Simple dependencies
def test_simple_dependencies():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1"),
CodeRequest(id="1", code="b = a + 2"),
CodeRequest(id="2", code="c = b + 3")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1", cellType='code'),
CodeRequest(id="1", code="b = a + 2", cellType='code'),
CodeRequest(id="2", code="c = b + 3", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -26,9 +26,9 @@ def test_simple_dependencies():

# Test 3: Complex dependencies
def test_complex_dependencies():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1"),
CodeRequest(id="1", code="b = a + 2"),
CodeRequest(id="2", code="c = b + a")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1", cellType='code'),
CodeRequest(id="1", code="b = a + 2", cellType='code'),
CodeRequest(id="2", code="c = b + a", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -38,10 +38,10 @@ def test_complex_dependencies():

# Test 4: Overriding dependencies
def test_overriding_dependencies():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1"),
CodeRequest(id="1", code="b = a + 2"),
CodeRequest(id="2", code="a = 3"),
CodeRequest(id="3", code="c = a + b")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1", cellType='code'),
CodeRequest(id="1", code="b = a + 2", cellType='code'),
CodeRequest(id="2", code="a = 3", cellType='code'),
CodeRequest(id="3", code="c = a + b", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -52,9 +52,9 @@ def test_overriding_dependencies():

# Test 5: Function definitions and calls
def test_function_definitions_and_calls():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="def f(x): return x + 2"),
CodeRequest(id="1", code="a = f(2)"),
CodeRequest(id="2", code="b = a + 3")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="def f(x): return x + 2", cellType='code'),
CodeRequest(id="1", code="a = f(2)", cellType='code'),
CodeRequest(id="2", code="b = a + 3", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -64,11 +64,11 @@ def test_function_definitions_and_calls():

# Test 6: Dependencies with function calls and redefinitions
def test_dependencies_with_function_calls_and_redefinitions():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="def f(x): return x + 2"),
CodeRequest(id="1", code="a = f(2)"),
CodeRequest(id="2", code="b = a + 3"),
CodeRequest(id="3", code="def g(x): return x * 2"),
CodeRequest(id="4", code="c = g(b)")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="def f(x): return x + 2", cellType='code'),
CodeRequest(id="1", code="a = f(2)", cellType='code'),
CodeRequest(id="2", code="b = a + 3", cellType='code'),
CodeRequest(id="3", code="def g(x): return x * 2", cellType='code'),
CodeRequest(id="4", code="c = g(b)", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -80,8 +80,8 @@ def test_dependencies_with_function_calls_and_redefinitions():

# Test 7: Importing a module in one cell and using it in another
def test_importing_module():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="import math"),
CodeRequest(id="1", code="a = math.sqrt(4)")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="import math", cellType='code'),
CodeRequest(id="1", code="a = math.sqrt(4)", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -90,8 +90,8 @@ def test_importing_module():

# Test 8: Defining multiple variables in one cell
def test_multiple_variables_in_one_cell():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1; b = 2; c = 3"),
CodeRequest(id="1", code="d = a + b + c")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1; b = 2; c = 3", cellType='code'),
CodeRequest(id="1", code="d = a + b + c", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -100,8 +100,8 @@ def test_multiple_variables_in_one_cell():

# Test 9: Multiline cells
def test_multiline_cells():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1\nb = 2\nc = 3"),
CodeRequest(id="1", code="d = a + b\ne = c * d")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1\nb = 2\nc = 3", cellType='code'),
CodeRequest(id="1", code="d = a + b\ne = c * d", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand All @@ -110,8 +110,8 @@ def test_multiline_cells():

# Test 10: Importing a module with an alias in one cell and using it in another
def test_importing_module_with_alias():
cells = Request(originId="0", cells=[CodeRequest(id="0", code="import math as m"),
CodeRequest(id="1", code="a = m.sqrt(4)")],
cells = Request(originId="0", cells=[CodeRequest(id="0", code="import math as m", cellType='code'),
CodeRequest(id="1", code="a = m.sqrt(4)", cellType='code')],
components={})
cell_dict = build_dependency_graph(parse_cells(cells))

Expand Down
10 changes: 5 additions & 5 deletions zt_backend/tests/test_execute_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ def setup_function():
# Test for simple variable assignment
def test_execute_request_simple_variable():
setup_function()
req = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1")], components={})
req = Request(originId="0", cells=[CodeRequest(id="0", code="a = 1", cellType='code')], components={})
execute_request(req)
assert 'a' in cell_outputs_dict['0']
assert cell_outputs_dict['0']['a'] == 1

# Test for function definition
def test_execute_request_function_definition():
setup_function()
req = Request(originId="1", cells=[CodeRequest(id="1", code="def add(x, y): return x + y")], components={})
req = Request(originId="1", cells=[CodeRequest(id="1", code="def add(x, y): return x + y", cellType='code')], components={})
execute_request(req)
assert 'add' in cell_outputs_dict['1']

# Test for importing modules
def test_execute_request_import_module():
setup_function()
req = Request(originId="2", cells=[CodeRequest(id="2", code="import math")], components={})
req = Request(originId="2", cells=[CodeRequest(id="2", code="import math", cellType='code')], components={})
execute_request(req)
assert 'math' in cell_outputs_dict['2']

# Test for multiple cells with dependencies
def test_execute_request_multiple_cells_with_dependencies():
setup_function()
req = Request(originId="3", cells=[CodeRequest(id="3", code="a = 1"),
CodeRequest(id="4", code="b = a + 1")], components={})
req = Request(originId="3", cells=[CodeRequest(id="3", code="a = 1", cellType='code'),
CodeRequest(id="4", code="b = a + 1", cellType='code')], components={})
execute_request(req)
assert 'a' in cell_outputs_dict['3']
assert 'b' in cell_outputs_dict['4']
Expand Down
5 changes: 4 additions & 1 deletion zt_dev_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import argparse
import subprocess
import os
import threading
import shutil
from zt_backend.models.generate_schema import generate_schema

def generate_ts():
os.mkdir('zt_schema')
generate_schema()
os.chdir('zt_frontend')
shutil.rmtree('src/types')
os.system('yarn json2ts -i ../zt_schema -o src/types')
os.chdir('..')
shutil.rmtree('zt_schema')

def start_servers(args):
if args.mode == 'app':
Expand Down
2 changes: 1 addition & 1 deletion zt_frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default {
const cellRequests: CodeRequest[] = []
const requestComponents: { [key: string]: any } = {};
for (let key in this.notebook.cells){
const cellRequest: CodeRequest = {id: key, code: this.notebook.cells[key].code}
const cellRequest: CodeRequest = {id: key, code: this.notebook.cells[key].code, cellType: this.notebook.cells[key].cellType}
for (const c of this.notebook.cells[key].components){
requestComponents[c.id] = c.value
}
Expand Down
2 changes: 2 additions & 0 deletions zt_frontend/src/shims-vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//Hack for typescript support for following package
declare module 'vue3-markdown-it';
2 changes: 2 additions & 0 deletions zt_frontend/src/types/request.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
export type Originid = string;
export type Id = string;
export type Code = string;
export type Celltype = "code" | "markdown" | "text";
export type Cells = CodeRequest[];

export interface Request {
Expand All @@ -19,6 +20,7 @@ export interface Request {
export interface CodeRequest {
id: Id;
code: Code;
cellType: Celltype;
[k: string]: unknown;
}
export interface Components {
Expand Down
21 changes: 0 additions & 21 deletions zt_frontend/src/types/zt_component.d.ts

This file was deleted.

28 changes: 0 additions & 28 deletions zt_schema/component_request.json

This file was deleted.

18 changes: 0 additions & 18 deletions zt_schema/create_request.json

This file was deleted.

13 changes: 0 additions & 13 deletions zt_schema/delete_request.json

This file was deleted.

Loading

0 comments on commit 72310c1

Please sign in to comment.