reflex
reflex copied to clipboard
Create pc.dict and pc.list
Currently Pynecone doesn't detect mutations in list/dict state vars which is annoying and can lead to bugs if one is not careful.
Maybe we could create classes pc.dict
and pc.list
that wrap any dict/list State vars, and modify them such that they detect mutations.
I can work on this. Can you provide an example how pc.dict
& pc.list
would look like? My guess would be sth like this:
def index():
colors = pc.list(['red', 'oragen'])
return pc.vstack(
pc.text('Number of colors:' + len(colors)),
pc.button('+', on_click=lambda : colors.append('another color'))
)
There was some discussion on Discord about that, but the idea would be to use pc.list
and pc.dict
behind the hood.
If you declare:
class State(pc.State):
somevar: list[str] = []
then using State.somevar should behave like a list, even if we actually replace it by a pc.list
(same thing with dict)
But maybe it can be done in two step:
- Making the actual classes
pc.list
andpc.dict
(so they can be used explicitly) - Use them implicitly in
pc.State
Yeh, we can use a real list / dict to store data, and rewrite __setitem__ to detect any change to it.
@TommyDew42 implemented pc.list
, now we just need pc.dict
Had to comment this out temporarily due to an issue with pickling the pc.list, which we need for Redis. Will prioritize getting this back in.
Had to comment this out temporarily due to an issue with pickling the pc.list, which we need for Redis. Will prioritize getting this back in.
Is there an issue created for this?
We need to use the actual name PCList
instead of pc.list
, because the latter refer to the List component in pynecone/components/datadisply/list.py
Since PCList
is used completely internally ( pynecone users should not have to declare a PCList ) then it doesn't need a shortcut in this style : pc.*
And for consistency, refer to the dict
wrapper as PCDict
.