Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 25, 2024
1 parent 6398735 commit b69f9a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 42 deletions.
4 changes: 3 additions & 1 deletion docs/examples/getting_started/shiny_express_all_new_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pytabulator import TableOptions, Tabulator, TabulatorContext, render_tabulator
from pytabulator.utils import create_columns
from pytabulator.formatters import ProgressFormatter, TickCrossFormatter
from pytabulator.editors import ListEditor
from pytabulator.editors import ListEditor, InputEditor, ProgressEditor
from shiny import reactive, render
from shiny.express import input, ui

Expand Down Expand Up @@ -85,6 +85,8 @@ def tabulator():
.set_column_formatter("Fare", ProgressFormatter(), hoz_align="left")
.set_column_formatter("Survived", TickCrossFormatter(), hoz_align="center")
.set_column_editor("Sex", ListEditor())
.set_column_editor("Name", InputEditor())
.set_column_editor("Fare", ProgressEditor(), hoz_align="left")
)


Expand Down
8 changes: 8 additions & 0 deletions pytabulator/_abstracts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pydantic import BaseModel

from ._utils import as_camel_dict_recursive


class MyBaseModel(BaseModel):
def to_dict(self) -> dict:
return as_camel_dict_recursive(self.model_dump(exclude_none=True))
63 changes: 22 additions & 41 deletions pytabulator/editors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from pydantic import BaseModel

from typing import Optional, Literal

from ._utils import as_camel_dict_recursive
from enum import Enum
from typing import Literal, Optional

from ._abstracts import MyBaseModel


class Editors(Enum):
Expand All @@ -17,38 +15,35 @@ class Editors(Enum):
LIST = "list"


class Editor(BaseModel):
class Editor(MyBaseModel):
_name: str = ""

@property
def name(self) -> str:
return ""

def to_dict(self) -> dict:
return as_camel_dict_recursive(self.model_dump(exclude_none=True))
return self._name


class InputEditor(Editor):
_name: str = Editors.INPUT.value

search: Optional[bool] = None
mask: Optional[str] = None
select_contents: Optional[bool] = None
element_attributes: Optional[dict] = None

@property
def name(self) -> str:
return Editors.INPUT.value


class TextareaEditor(Editor):
_name: str = Editors.TEXTAREA.value

mask: Optional[str] = None
select_contents: Optional[bool] = None
vertical_navigation: Literal["hybrid", "editor", "table"] = None
shift_enter_submit: Optional[bool] = None

@property
def name(self) -> str:
return Editors.TEXTAREA.value


class NumberEditor(Editor):
_name: str = Editors.NUMBER.value

min: Optional[float] = None
max: Optional[float] = None
step: Optional[float] = None
Expand All @@ -57,52 +52,38 @@ class NumberEditor(Editor):
select_contents: Optional[bool] = None
vertical_navigation: Literal["editor", "table"] = None

@property
def name(self) -> str:
return Editors.NUMBER.value


class RangeEditor(Editor):
_name: str = Editors.RANGE.value

min: Optional[float] = None
max: Optional[float] = None
step: Optional[float] = None
element_attributes: Optional[dict] = None

@property
def name(self) -> str:
return Editors.RANGE.value


class TickCrossEditor(Editor):
_name: str = Editors.TICK_CROSS.value

true_value: Optional[str] = None
false_value: Optional[str] = None
element_attributes: Optional[dict] = None

@property
def name(self) -> str:
return Editors.TICK_CROSS.value


class StarEditor(Editor):
@property
def name(self) -> str:
return Editors.STAR.value
_name: str = Editors.STAR.value


class ProgressEditor(Editor):
_name: str = Editors.PROGRESS.value

min: Optional[float] = None
max: Optional[float] = None
element_attributes: Optional[dict] = None

@property
def name(self) -> str:
return Editors.PROGRESS.value


class ListEditor(Editor):
_name: str = Editors.LIST.value

values: Optional[list] = None
values_lookup: Optional[bool] = True

@property
def name(self) -> str:
return Editors.LIST.value

0 comments on commit b69f9a5

Please sign in to comment.