[Python] Access states as plain Python value for serialization

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the Python reflex package to build a frontend (https://github.com/reflex-dev/reflex). I am trying to serialize the state values on a button click, which does not seem to work as expected. The values seem to be encapsulated in something non-serializable.

    How do I get access to the "raw" value of a state?


    TypeError: Object of type StringCastedVar is not JSON serializable
    ​

    class StateA(rx.State):
    value: str = "Hello"

    class StateB(rx.State):
    value: str = "World"

    class ConfirmButton(rx.State):
    did_confirm: bool = False


    @rx.event
    def confirm(self):
    self.did_confirm = True
    import json
    with open("x.json", mode='w') as h:
    h.write(json.dumps({
    "word1": StateA.value,
    "word2": StateB.value,
    }))

    Continue reading...

Compartilhe esta Página