MCPcopy Index your code
hub / github.com/reactive-python/reactpy / module_from_string

Function module_from_string

src/py/reactpy/reactpy/web/module.py:242–298  ·  view source on GitHub ↗

Load a :class:`WebModule` whose ``content`` comes from a string. Parameters: name: The name of the package content: The contents of the web module fallback: What to temporarily display while the module is being loaded. resolve_

(
    name: str,
    content: str,
    fallback: Any | None = None,
    resolve_exports: bool | None = None,
    resolve_exports_depth: int = 5,
    unmount_before_update: bool = False,
)

Source from the content-addressed store, hash-verified

240
241
242def module_from_string(
243 name: str,
244 content: str,
245 fallback: Any | None = None,
246 resolve_exports: bool | None = None,
247 resolve_exports_depth: int = 5,
248 unmount_before_update: bool = False,
249) -> WebModule:
250 """Load a :class:`WebModule` whose ``content`` comes from a string.
251
252 Parameters:
253 name:
254 The name of the package
255 content:
256 The contents of the web module
257 fallback:
258 What to temporarily display while the module is being loaded.
259 resolve_imports:
260 Whether to try and find all the named exports of this module.
261 resolve_exports_depth:
262 How deeply to search for those exports.
263 unmount_before_update:
264 Cause the component to be unmounted before each update. This option should
265 only be used if the imported package fails to re-render when props change.
266 Using this option has negative performance consequences since all DOM
267 elements must be changed on each render. See :issue:`461` for more info.
268 """
269 name += module_name_suffix(name)
270
271 target_file = _web_module_path(name)
272
273 if target_file.exists() and target_file.read_text(encoding="utf-8") != content:
274 logger.info(
275 f"Existing web module {name!r} will "
276 f"be replaced with {target_file.resolve()}"
277 )
278 target_file.unlink()
279
280 target_file.parent.mkdir(parents=True, exist_ok=True)
281 target_file.write_text(content)
282
283 return WebModule(
284 source=name,
285 source_type=NAME_SOURCE,
286 default_fallback=fallback,
287 file=target_file,
288 export_names=(
289 resolve_module_exports_from_file(target_file, resolve_exports_depth)
290 if (
291 resolve_exports
292 if resolve_exports is not None
293 else REACTPY_DEBUG_MODE.current
294 )
295 else None
296 ),
297 unmount_before_update=unmount_before_update,
298 )
299

Callers 1

module_from_templateFunction · 0.85

Calls 4

module_name_suffixFunction · 0.90
_web_module_pathFunction · 0.85
WebModuleClass · 0.85

Tested by

no test coverage detected