Skip to content

Commit 0be806e

Browse files
authored
Merge pull request #1302 from waralex/1189-julia-components
1189 julia components
2 parents 7eb5022 + 650b9d3 commit 0be806e

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

dash/development/_jl_components_generation.py

+44-37
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from ._all_keywords import julia_keywords
1414
from ._py_components_generation import reorder_props
1515

16+
# uuid of DashBase Julia package.
17+
jl_dash_base_uuid = "03207cf0-e2b3-4b91-9ca8-690cf0fb507e"
18+
1619
# uuid of Dash Julia package. Used as base for component package uuid
1720
jl_dash_uuid = "1b08a953-4be3-4667-9a23-3db579824955"
1821

@@ -23,59 +26,48 @@
2326
export {funcname}
2427
2528
"""
26-
{funcname}(;kwargs...)
27-
{funcname}(children::Any;kwargs...)
28-
{funcname}(children_maker::Function;kwargs...)
29+
{funcname}(;kwargs...){children_signatures}
2930
3031
{docstring}
3132
"""
3233
function {funcname}(; kwargs...)
33-
available_props = Set(Symbol[{component_props}])
34-
wild_props = Set(Symbol[{wildcard_symbols}])
35-
wild_regs = r"^(?<prop>{wildcard_names})"
36-
37-
result = Component("{element_name}", "{module_name}", Dict{{Symbol, Any}}(), available_props, Set(Symbol[{wildcard_symbols}]))
38-
39-
for (prop, value) = pairs(kwargs)
40-
m = match(wild_regs, string(prop))
41-
if (length(wild_props) == 0 || isnothing(m)) && !(prop in available_props)
42-
throw(ArgumentError("Invalid property $(string(prop)) for component " * "{funcname}"))
43-
end
44-
45-
push!(result.props, prop => Front.to_dash(value))
46-
end
47-
48-
return result
34+
available_props = Symbol[{component_props}]
35+
wild_props = Symbol[{wildcard_symbols}]
36+
return Component("{funcname}", "{element_name}", "{module_name}", available_props, wild_props; kwargs...)
4937
end
38+
{children_definitions}
39+
''' # noqa:E501
5040

51-
function {funcname}(children::Any; kwargs...)
52-
result = {funcname}(;kwargs...)
53-
push!(result.props, :children => Front.to_dash(children))
54-
return result
55-
end
41+
jl_children_signatures = '''
42+
{funcname}(children::Any;kwargs...)
43+
{funcname}(children_maker::Function;kwargs...)
44+
'''
5645

46+
jl_children_definitions = '''
47+
{funcname}(children::Any; kwargs...) = {funcname}(;kwargs..., children = children)
5748
{funcname}(children_maker::Function; kwargs...) = {funcname}(children_maker(); kwargs...)
58-
''' # noqa:E501
49+
'''
5950

6051
jl_package_file_string = '''
6152
module {package_name}
62-
using Dash
53+
using {base_package}
6354
6455
const resources_path = realpath(joinpath( @__DIR__, "..", "deps"))
6556
const version = "{version}"
6657
6758
{component_includes}
6859
6960
function __init__()
70-
Dash.register_package(
71-
Dash.ResourcePkg(
61+
DashBase.register_package(
62+
DashBase.ResourcePkg(
7263
"{project_shortname}",
7364
resources_path,
7465
version = version,
7566
[
7667
{resources_dist}
7768
]
7869
)
70+
7971
)
8072
end
8173
end
@@ -87,23 +79,24 @@
8779
{authors}version = "{version}"
8880
8981
[deps]
90-
Dash = "{dash_uuid}"
82+
{base_package} = "{dash_uuid}"
9183
9284
[compact]
93-
julia = "1.1"
94-
Dash = ">=0.1"
85+
julia = "1.2"
86+
{base_package} = ">=0.1"
9587
'''
9688

9789
jl_component_include_string = 'include("{name}.jl")'
9890

99-
jl_resource_tuple_string = '''Dash.Resource(
91+
jl_resource_tuple_string = '''DashBase.Resource(
10092
relative_package_path = {relative_package_path},
10193
external_url = {external_url},
10294
dynamic = {dynamic},
10395
async = {async_string},
10496
type = :{type}
10597
)'''
10698

99+
core_packages = ['dash_html_components', 'dash_core_components', 'dash_table']
107100

108101
def jl_package_name(namestring):
109102
s = namestring.split("_")
@@ -370,6 +363,14 @@ def nothing_or_string(v):
370363
else 'nothing'
371364
) for resource in resources]
372365

366+
def is_core_package(project_shortname):
367+
return project_shortname in core_packages
368+
369+
def base_package_name(project_shortname):
370+
return "DashBase" if is_core_package(project_shortname) else "Dash"
371+
372+
def base_package_uid(project_shortname):
373+
return jl_dash_base_uuid if is_core_package(project_shortname) else jl_base_uuid
373374

374375
def generate_package_file(project_shortname, components, pkg_data, prefix):
375376
package_name = jl_package_name(project_shortname)
@@ -391,15 +392,15 @@ def generate_package_file(project_shortname, components, pkg_data, prefix):
391392
),
392393
resources_dist=resources_dist,
393394
version=project_ver,
394-
project_shortname=project_shortname
395+
project_shortname=project_shortname,
396+
base_package=base_package_name(project_shortname)
395397

396398
)
397399
file_path = os.path.join("src", package_name + ".jl")
398400
with open(file_path, "w") as f:
399401
f.write(package_string)
400402
print("Generated {}".format(file_path))
401403

402-
403404
def generate_toml_file(project_shortname, pkg_data):
404405
package_author = pkg_data.get("author", "")
405406
project_ver = pkg_data.get("version")
@@ -414,14 +415,14 @@ def generate_toml_file(project_shortname, pkg_data):
414415
package_uuid=package_uuid,
415416
version=project_ver,
416417
authors=authors_string,
417-
dash_uuid=jl_dash_uuid
418+
base_package=base_package_name(project_shortname),
419+
dash_uuid=base_package_uid(project_shortname),
418420
)
419421
file_path = "Project.toml"
420422
with open(file_path, "w") as f:
421423
f.write(toml_string)
422424
print("Generated {}".format(file_path))
423425

424-
425426
def generate_class_string(name, props, description, project_shortname, prefix):
426427
# Ensure props are ordered with children first
427428
filtered_props = reorder_props(filter_props(props))
@@ -430,7 +431,7 @@ def generate_class_string(name, props, description, project_shortname, prefix):
430431

431432
docstring = create_docstring_jl(
432433
component_name=name, props=filtered_props, description=description
433-
).replace("\r\n", "\n")
434+
).replace("\r\n", "\n").replace('$', '\$')
434435

435436
wclist = get_wildcards_jl(props)
436437
default_paramtext = ""
@@ -453,6 +454,10 @@ def generate_class_string(name, props, description, project_shortname, prefix):
453454
for p in prop_keys
454455
)
455456

457+
has_children = "children" in prop_keys
458+
funcname = format_fn_name(prefix, name)
459+
children_signatures = jl_children_signatures.format(funcname=funcname) if has_children else ""
460+
children_definitions = jl_children_definitions.format(funcname=funcname) if has_children else ""
456461
return jl_component_string.format(
457462
funcname=format_fn_name(prefix, name),
458463
docstring=docstring,
@@ -461,6 +466,8 @@ def generate_class_string(name, props, description, project_shortname, prefix):
461466
wildcard_names=stringify_wildcards(wclist, no_symbol=True),
462467
element_name=name,
463468
module_name=project_shortname,
469+
children_signatures = children_signatures,
470+
children_definitions = children_definitions
464471
)
465472

466473

0 commit comments

Comments
 (0)