From 0bf6addf44cb645b23ada32a5745d7946620bf3f Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Tue, 1 Oct 2024 16:49:47 -0700 Subject: [PATCH] [ENG-3870] rx.call_script with f-string var produces incorrect code Avoid casting javascript code with embedded Var as LiteralStringVar There are two cases that need to be handled: 1. The javascript code contains Vars with VarData, these can only be evaluated in the component context, since they may use hooks. Vars with VarData cannot be used from the backend. In this case, we cast the given code as a raw js expression and include the extracted VarData. 2. The javascript code has no VarData. In this case, we pass the code as the raw js expression and cast to a python str to get a js literal string to eval. --- reflex/event.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/reflex/event.py b/reflex/event.py index 95358ace1..a059e7a47 100644 --- a/reflex/event.py +++ b/reflex/event.py @@ -839,6 +839,16 @@ def call_script( ), ), } + if isinstance(javascript_code, str): + # When there is VarData, include it and eval the JS code inline on the client. + javascript_code, original_code = ( + LiteralVar.create(javascript_code), + javascript_code, + ) + if javascript_code._get_all_var_data() is None: + # Without VarData, cast to string and eval the code in the event loop. + javascript_code = str(Var(_js_expr=original_code)) + return server_side( "_call_script", get_fn_signature(call_script),