Skip to content

Commit

Permalink
support globbing when defining dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Jul 11, 2023
1 parent efb4026 commit 605ca92
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
33 changes: 26 additions & 7 deletions nicegui/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,34 @@ def __init_subclass__(cls, *,
exposed_libraries: List[Union[str, Path]] = [],
extra_libraries: List[Union[str, Path]] = [],
) -> None:
def abs_path(file: Union[str, Path]) -> Path:
p = Path(file)
return p if p.is_absolute() else base / p
super().__init_subclass__()
base = Path(inspect.getfile(cls)).parent
cls.component = register_vue_component(abs_path(component)) if component else None
cls.libraries = [register_library(abs_path(library)) for library in libraries]
cls.extra_libraries = [register_library(abs_path(library)) for library in extra_libraries]
cls.exposed_libraries = [register_library(abs_path(library), expose=True) for library in exposed_libraries]

def glob_absolute_paths(file: Union[str, Path]) -> List[Path]:
path = Path(file)
if not path.is_absolute():
path = base / path
return sorted(path.parent.glob(path.name), key=lambda p: p.stem)

cls.component = None
if component:
for path in glob_absolute_paths(component):
cls.component = register_vue_component(path)

cls.libraries = []
for library in libraries:
for path in glob_absolute_paths(library):
cls.libraries.append(register_library(path))

cls.extra_libraries = []
for library in extra_libraries:
for path in glob_absolute_paths(library):
cls.extra_libraries.append(register_library(path))

cls.exposed_libraries = []
for library in exposed_libraries:
for path in glob_absolute_paths(library):
cls.exposed_libraries.append(register_library(path, expose=True))

def add_slot(self, name: str, template: Optional[str] = None) -> Slot:
"""Add a slot to the element.
Expand Down
10 changes: 4 additions & 6 deletions nicegui/elements/chart.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from pathlib import Path
from typing import Dict, List

from ..element import Element

base = Path(__file__).parent
libraries = [p.relative_to(base) for p in sorted((base / 'lib' / 'highcharts').glob('*.js'), key=lambda p: p.stem)]
modules = {p.stem: p.relative_to(base) for p in sorted((base / 'lib' / 'highcharts' / 'modules').glob('*.js'))}


class Chart(Element, component='chart.js', libraries=libraries, extra_libraries=list(modules.values())):
class Chart(Element,
component='chart.js',
libraries=['lib/highcharts/*.js'],
extra_libraries=['lib/highcharts/modules/*.js']):

def __init__(self, options: Dict, *, type: str = 'chart', extras: List[str] = []) -> None:
"""Chart
Expand Down
6 changes: 1 addition & 5 deletions nicegui/elements/mermaid.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from pathlib import Path

from .mixins.content_element import ContentElement

base = Path(__file__).parent


class Mermaid(ContentElement,
component='mermaid.js',
exposed_libraries=['lib/mermaid/mermaid.esm.min.mjs'],
extra_libraries=[p.relative_to(base) for p in (base / 'lib' / 'mermaid').glob('*.js')]):
extra_libraries=['lib/mermaid/*.js']):
CONTENT_PROP = 'content'

def __init__(self, content: str) -> None:
Expand Down

0 comments on commit 605ca92

Please sign in to comment.