1- import urllib .parse
2- from importlib import import_module
31from typing import ClassVar
42from uuid import uuid4
53
64from jinja2_simple_tags import StandaloneTag
75
86from reactpy .utils import render_mount_template
97
10- try :
11- import_module ("jinja2" )
12- except ImportError as e :
13- raise ImportError (
14- "The Jinja2 library is required to use the ReactPy template tag. "
15- "Please install it via `pip install reactpy[jinja]`."
16- ) from e
17-
188
199class ReactPyTemplateTag (StandaloneTag ):
2010 """This allows enables a `component` tag to be used in any Jinja2 rendering context,
@@ -23,18 +13,9 @@ class ReactPyTemplateTag(StandaloneTag):
2313 safe_output = True
2414 tags : ClassVar [set [str ]] = {"component" }
2515
26- def render (self , dotted_path : str , * args , ** kwargs ):
27- uuid = uuid4 ().hex
28- class_ = kwargs .pop ("class" , "" )
29- kwargs .pop ("key" , "" ) # `key` is effectively useless for the root node
30-
31- # Generate the websocket URL
32- append_component_path = f"{ dotted_path } /"
33- if kwargs .get ("args" ) is not None :
34- raise ValueError ("Cannot specify `args` as a keyword argument" )
35- if args :
36- kwargs ["args" ] = args
37- if kwargs :
38- append_component_path += f"?{ urllib .parse .urlencode (kwargs )} "
39-
40- return render_mount_template (uuid , class_ , append_component_path )
16+ def render (self , dotted_path : str , ** kwargs ):
17+ return render_mount_template (
18+ element_id = uuid4 ().hex ,
19+ class_ = kwargs .pop ("class" , "" ),
20+ append_component_path = f"{ dotted_path } /" ,
21+ )
0 commit comments