Skip to content

Commit

Permalink
Refactor widget
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Jan 20, 2023
1 parent 16defe6 commit cebd15e
Show file tree
Hide file tree
Showing 19 changed files with 417 additions and 234 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,6 @@ src/_interface/
# Integration tests
ui-tests/test-results/
ui-tests/playwright-report/

# Hatchling
jupytercad/_version.py
39 changes: 17 additions & 22 deletions examples/api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,42 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from jupytercad.widget import Document"
"from jupytercad.notebook import CadDocument"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = Document(\"examples/common.FCStd\")"
"a = CadDocument(\"examples/common.FCStd\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5a02cb81887e48e294805571aaa81169",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Document(path='examples/common.FCStd')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from jupytercad.notebook import Box\n",
"box = Box(10,2,2)\n",
"a.add_object(box)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
1 change: 1 addition & 0 deletions jupytercad/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._version import __version__
from .notebook import CadDocument

def _jupyter_labextension_paths():
return [{'src': 'labextension', 'dest': 'jupytercad'}]
2 changes: 2 additions & 0 deletions jupytercad/notebook/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .cad_document import CadDocument
from .objects import *
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from cmath import log
from typing import Dict
from typing import Dict, List


from ipywidgets import DOMWidget
from traitlets import Unicode

from .objects import BaseObject

from .utils import MESSAGE_ACTION
from ._frontend import module_name, module_version

class Document(DOMWidget):

class CadDocument(DOMWidget):

_model_name = Unicode('JupyterCadWidgetModel').tag(sync=True)
_model_module = Unicode(module_name).tag(sync=True)
Expand All @@ -20,4 +24,14 @@ class Document(DOMWidget):
def __init__(self, path: str, **kwargs) -> None:
super().__init__(**kwargs)
self.path = path

self.on_msg(self._handle_frontend_msg)

def _handle_frontend_msg(
self, model: 'CadDocument', msg: Dict, buffer: List
) -> None:
pass

def add_object(self, object: BaseObject):
self.send(
{'action': MESSAGE_ACTION.ADD_OBJECT, 'payload': object.to_dict()}
)
3 changes: 3 additions & 0 deletions jupytercad/notebook/objects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .box import Box
from .placement import Placement
from .base import BaseObject
11 changes: 11 additions & 0 deletions jupytercad/notebook/objects/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from abc import ABC, abstractmethod
from typing import Dict

class BaseObject(ABC):
@abstractmethod
def to_dict(self) -> Dict:
pass

@abstractmethod
def from_dict(self, value: Dict) -> None:
pass
30 changes: 30 additions & 0 deletions jupytercad/notebook/objects/box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Dict

from .placement import Placement
from .base import BaseObject


class Box(BaseObject):
def __init__(
self, Length=1, Width=1, Height=1, Placement=Placement()
) -> None:
super().__init__()
self.Length = Length
self.Width = Width
self.Height = Height
self.Placement = Placement

def from_dict(self, value: Dict) -> None:
self.Length = value.get('Length', None)
self.Width = value.get('Width', None)
self.Height = value.get('Height', None)
newPlacement = Placement()
self.Placement = newPlacement.from_dict(value.get('Placement', None))

def to_dict(self) -> Dict:
return dict(
Length=self.Length,
Width=self.Width,
Height=self.Height,
Placement=self.Placement.to_dict(),
)
19 changes: 19 additions & 0 deletions jupytercad/notebook/objects/placement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import Dict
from .base import BaseObject


class Placement(BaseObject):

def __init__(self, Position=[0,0,0], Axis=[1,0,0], Angle= 0) -> None:
super().__init__()
self.Position = Position
self.Axis = Axis
self.Angle = Angle

def to_dict(self) -> Dict:
return dict(Position=self.Position, Axis=self.Axis, Angle=self.Angle)

def from_dict(self, value: Dict) -> None:
self.Position = value.get('Position', None)
self.Axis = value.get('Axis', None)
self.Angle = value.get('Angle', None)
5 changes: 5 additions & 0 deletions jupytercad/notebook/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from enum import Enum


class MESSAGE_ACTION(str, Enum):
ADD_OBJECT = 'add_object'
1 change: 0 additions & 1 deletion jupytercad/widget/__init__.py

This file was deleted.

Loading

0 comments on commit cebd15e

Please sign in to comment.