[Pynecone] 2.gallery-to-do

rebugger·2023년 2월 6일
0

pynecone

목록 보기
2/5

import pynecone as pc

class State(pc.State):
    items = ['Write Code', 'Sleep', 'Have Fun']
    new_item:str = ""
    input_string:str = "" #초기화용

    def add_item(self):
        self.items += [self.new_item]
        self.new_item = ""

    def remove_item(self, item):
        self.items.remove(item)
        self.items = [i for i in self.items]

    def clear_input(self):
        self.input_string = ""
        
def render_item(item):
    return pc.list_item(
        pc.hstack(
            pc.button(
                on_click = lambda : State.remove_item(item), 
                bg="white", 
                border="1px solid blue",
                height = "1em"
            ),
            pc.text(item)
        ),
        font_size = "1em",
    )   

def index():
	return pc.container(
        pc.vstack(
            pc.heading("Todos"),
            pc.input(
                value=State.input_string,
                placeholder="Add a todo...",
                on_change=State.set_input_string,
                on_blur=State.set_new_item,
                bg="white"
            ),
            pc.button("Add", on_click=State.add_item, bg="white", on_mouse_up=State.clear_input), #<= onclick -> on_mouse_up 실행됨
            pc.divider(),
            pc.ordered_list(
                pc.foreach(State.items, lambda item : render_item(item)) 
            ),
            bg = "#eeeeee",
            border_radius = "1em",
            padding = "1em",
            margin = "1em",
            shadow="lg",
        ),
        bg = "black",
        border_radius="1em",
    )
		

app = pc.App(state=State)
app.add_page(index)
app.compile()
profile
모르는게 많고, 부족함이 많다는 것을 인정하기

0개의 댓글