writer / writer-framework

No-code in the front, Python in the back. An open-source framework for creating data apps.
https://dev.writer.com/framework/introduction
Apache License 2.0
1.31k stars 76 forks source link

Set cookies? #578

Open Xynonners opened 6 days ago

Xynonners commented 6 days ago

Hi,

This framework is great (most ergonomic by far of the pure python ones I've tested), but I'm trying to find a way to modify cookies and store data locally on the client side. There doesn't seem to be a properly supported method of doing this currently?

I attempted to mutate the cookies in the session dictionary, but the changes were not propagated to the client side.

Is there any way to do this? Thanks.

FabienArcellier commented 4 days ago

We haven't developped anything about this currently.

We discussed allowing information to be stored in the browser a few months ago. We have excluded writing cookies due to WF design.

WF initiates the connection then the exchanges go through a websocket connection. a cookie cannot be written to the browser. The table in session is an image of cookies. To write a new cookie, the server need to answer an http request. There is none between the backend and the frontend.

We discussed the possibility of writing to local storage. One possibility to be useful would be for the browser to send the content of local storage to the server during initialization and to be available in the session attribute.

image

Example of API

def on_app_init(state, session):
    state['user_settings']['bgcolor'] = session.local_storage.get('ui_settings', {}).get('bg_color', 'white')

def configure_ui_settings(state, payload, session):
    ui_settings = session.local_storage['ui_settings']
    ui_settings['bg_color'] = payload
    state.set_local_storage('ui_settings', ui_settings) # accept only serializable type

def reset_ui_settings(state, session):
    state.unset_local_storage('ui_settings')

# API
session.local_storage # dict with the content of wf_store from local_storage
session.set_local_storage # set a new value on localstorage
session.unset_local_storage # unset a key from localstorage
Xynonners commented 3 days ago

We haven't developped anything about this currently.

We discussed allowing information to be stored in the browser a few months ago. We have excluded writing cookies due to WF design.

WF initiates the connection then the exchanges go through a websocket connection. a cookie cannot be written to the browser. The table in session is an image of cookies. To write a new cookie, the server need to answer an http request. There is none between the backend and the frontend.

We discussed the possibility of writing to local storage. One possibility to be useful would be for the browser to send the content of local storage to the server during initialization and to be available in the session attribute.

image

Example of API

def on_app_init(state, session):
    state['user_settings']['bgcolor'] = session.local_storage.get('ui_settings', {}).get('bg_color', 'white')

def configure_ui_settings(state, payload, session):
    ui_settings = session.local_storage['ui_settings']
    ui_settings['bg_color'] = payload
    state.set_local_storage('ui_settings', ui_settings) # accept only serializable type

def reset_ui_settings(state, session):
    state.unset_local_storage('ui_settings')

# API
session.local_storage # dict with the content of wf_store from local_storage
session.set_local_storage # set a new value on localstorage
session.unset_local_storage # unset a key from localstorage

really just having any way of storing data on the frontend would be very useful, cookies or local storage.

could be used for a lot of things like storing models, settings, and session persistence / tokens / etc (things I want to do)