Comments (11)
I suspect
For example, "{var}" will be unwrapped to "var".
in unwrap_vars,
specifcally this:
{(.*?)} # extract the value between curly braces (non-greedy)
from the regex
and if I remove format.unwrap_vars from the serialize_list callback I get this:
{"key": "1", "value": "text1"}
["{"key": "1", "value": "text1"}"]
so that is a smoking gun...
removing this stops the curly braces from disappearing
{(.*?)} # extract the value between curly braces (non-greedy)
from reflex.
we need to unwrap vars inside lists and dicts when they are real JS values (like functions), but for things that serialize to dictionaries, the unwrapping is taking away the outer curly braces where it shouldn't.
from reflex.
yeah, so you need to know either the format of a var string so you can more precisely target it, with the regex.
Or you need to somehow leverage the recursion that json.dumps creates, and maybe detect when you are serializing a var, so you can take away the curly braces at that point. I would prefer that second option, because it follows the semantics of what you are trying to serialize. Is there specific serialization logic for vars?
from reflex.
Maybe something like this in the super class of all vars? https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable
from reflex.
vars are serialized up front using reflex.utils.serializers and related functions... the _var_name
is already the serialized form
from reflex.
The problem with touching anything in this formatting code is that it's going to have fallout for other parts of the framework that have been working fine. Any changes must be very carefully considered to limit the scope of the change to the bug in question without introducing other regressions.
The entire Var system is begging for a refactor because it's too hard to special case serialization for different types of vars. I was just running into this myself while trying to get nested dict
serialization to work.
from reflex.
Whenever I am in doubt whether it is time for a refactor it is usually means I should have refactored 2 weeks ago. Refactoring is often considered terrifying. The fear usually stems from the following reasoning: "Before I can refactor I must understand how this code works". But these days I take a different perspective: Refactoring is restructuring code that leads to understanding. I start by making small simplifications (that do not change behavior) and then work my way up, never making any big change. I also see refactoring as fundamentally different from solving bugs.
from reflex.
But... to work towards a solution: wouldn't removing the part of the regex while adding the following work?
@serializer
def serialize_var(var: rx.Var) -> str:
# solve the problem here
# You can make more fine grained serializers if you want
return format_var(var) # maybe not even format?
from reflex.
The serialization code in the reflex code looks pretty organized....
from reflex.
What should a var look like in the output?
from reflex.
Vars end up looking different based on some of their internal flags (_var_is_string
and _var_is_local
), but generally the str(my_var)
is the format that gets used in the generated code.
I mean, feel free to experiment and try to find a fix, but the tests must still pass and reflex-web must still build. The only changes in the tests should be additions and obviously justifiably wrong behavior.
I haven't really had a chance to investigate this one much yet, but I would probably start by adding a test case for it, and then making small tweaks to the code until the new test passes and none of the existing tests fail.
As for why def serialize_var(var: rx.Var) -> str:
seems weird to me, is that the Var.create
method already calls serialize
on whatever it gets passed, so in a sense, the Var already holds the serialized form, and AFAIK we don't pass any Var
objects to the serialize
method.
As for the refactoring, it's been a long time coming for this class, it needs a serious rethink to avoid all of the special cases and "external" format_*
methods that additionally tweak the output in certain circumstances. Ideally the Var
class itself would have subclasses for each type that needs special behavior, like VarDict
, VarStr
, VarBase
, etc; the base Var
would have the formatters for different output modes, like "prop" or "expression". Definitely need to solidify the refactor plan more to ensure the proposed API actually fixes the real problems that we face today.
from reflex.
Related Issues (20)
- add bare sqlalchemy rx.session
- [REF-3157] sqlmodel metaclass conflict when importing `sqlmodel` before `reflex.model` HOT 8
- File upload appears to hold everything in memory until the end
- [REF-3145] Allow component inheriting from a valid parent to pass validation check
- [REF-3146] call_script with callback is broken when used as an event trigger
- [REF-3148] rx.tabs is missing props inherited from the primitive
- Bug in rx.download() when we pass string with # in data argument HOT 3
- [REF-3160] [VarOps] It is way too easy to pass incorrect kwargs to `rx.Var._replace` HOT 1
- [REF-3164] Hot reload with py3.12 on Windows takes 45+ seconds HOT 1
- [REF-3165] Warning: Text content did not match. Server: "" Client: "system"
- [REF-3167] `rx.select` does not propagate props HOT 5
- [REF-3165] Warning: Text content did not match. Server: "" Client: "system"
- [suggestion] Python Code Security Plugin, Pixeebot (Free Installation for Open Source) HOT 5
- `rx.select(position="item-aligned")` propagates click events on mobile
- [REF-3184] All my rx.background don't work on 0.5.5
- [REF-3185] Expected unicode escape when rendering `rx.selected_files` in `rx.text`
- @lottiefiles/dotlottie-react: React component for rendering and controlling dotLottie animations in React applications.
- [REF-3190] Add testing to verify props propagation of all reflex components HOT 2
- [REF-3192] rx.foreach over computed var list fails HOT 1
- Alternative Methods for Launching a Reflex App in a Restricted Network Environment HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reflex.