FabienArcellier / writer-framework

No-code in the front, Python in the back. An open-source framework for creating data apps.
https://streamsync.cloud
Apache License 2.0
0 stars 1 forks source link

implement editable dataframe to manage dataframe editor component #65

Closed FabienArcellier closed 2 months ago

FabienArcellier commented 4 months ago
import writer as wf

def on_record_add(state, payload):
    payload.record['sales'] = 0 # default value inside the dataframe
    state['df'].record_add(payload)

def on_record_change(state, payload):
    state['df'].record_update(payload)

def on_record_action(state, payload):
    """
    This event corresponds to a quick action in the drop-down menu to the left of the dataframe.
    """
    if payload.action == 'remove':
        state['df'].record_remove(payload)
    if payload.action == 'important':
        state['df'].record(payload.id).update('flag', True) # update the column flag of the dataframe to true, trigger une mutation record_update
    if payload.action == 'open':
        state['record'] = state['df'].record(payload.id)

def on_record_select(state, payload):
    state['df_selected_record'] = payload['selected']

# trigger on button outside the dataframe editor
def on_save_click(state, payload):
    state['df'].df.to_csv('file.csv')

initial_state = ss.init_state({
    "df": wf.EditableDataframe(
        df
    )
})

# snippets
state['df'].df # get the real dataframe datastructure
state['df'].df = df # set a new dataframe, send the entire dataframe
state['df'].record_update(payload) # update a record using frontend payload
state['df'].record_remove(payload) # remove a record using frontend payload
state['df'].record_add(payload) # add a new record using frontend payload

more in #63