reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Create pc.dict and pc.list

Open picklelo opened this issue 2 years ago β€’ 4 comments

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.

picklelo avatar Jan 24 '23 16:01 picklelo

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'))
  )

TommyDew42 avatar Jan 24 '23 19:01 TommyDew42

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:

  1. Making the actual classes pc.list and pc.dict (so they can be used explicitly)
  2. Use them implicitly in pc.State

Lendemor avatar Jan 24 '23 23:01 Lendemor

Yeh, we can use a real list / dict to store data, and rewrite __setitem__ to detect any change to it.

FHU-yezi avatar Jan 25 '23 00:01 FHU-yezi

@TommyDew42 implemented pc.list, now we just need pc.dict

picklelo avatar Jan 28 '23 18:01 picklelo

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.

picklelo avatar Jan 31 '23 03:01 picklelo

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?

ElijahAhianyo avatar Feb 10 '23 10:02 ElijahAhianyo

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.

Lendemor avatar Feb 10 '23 10:02 Lendemor