Is it possible to get flask variable in dash js script?

Hi!

I added some external js to dash plotly which doesn’t affect anything at all but requires some additional info. More specific — i added flask-socketio socket. But i do need to connect each user to specific room with specific id which i obviously can get at backend, but i am struggling to get that on frontend. I could add some callback and so on, but it has some latency in render. My external js loads faster and i don’t want to wait for all callbacks to be triggered (cause i have a lot lol). I know i could add variable to the url, but it seems unsafe to me.

Any ideas?

Hello @vokiatik,

Why do you need flask info on the JS, typically requests will carry information already present on the browser.

1 Like

i generate room id for the socket connection at the backend and i need to specify it after connection to socketio server is established so user could connect to specific room.

This info isnt generated into a cookie that is returned in a response when registering?

1 Like

No, it generates each time user log in, so It could be different. However, I might consider your idea if we couldn’t figure it out rhis way.

Are you using a flask-login or something?

If so, the current user should already exist, in which case you should be able to use that.

1 Like

I tried, but i couldn’t find how to.

I am using external js file, where i write in plain jquery. So jinja syntax isn’t working outside flask. How exactly should i get current_user? Is it in request headers or something?

flask-login creates a cookie that has the session id and from there you can get the current user.

But, it depends on if your app is using it or not.

2 Likes

Sorry, i do use flask-login. I tried, but cookies are empty. Am i doing something wrong?

Assuming you are using a request to register, you should see the payload headers include them in the request.

Then you can read them from the flask.request.cookies on the server.

Also, if you have your cookies set to http-only, JS cant get them. :stuck_out_tongue:

1 Like

Sorry, that’s beyond my understanding(( It’s either my english is not good enough or my skills (could be both :rofl:).

By register you mean the root that is redirecting to dash app? I do use request to redirect users. Then, i guess, i do need to add extra header with user-related data to that redirect?

I might have, but i think cookies should be set to http-only bc of safity, aren’t they?

Haha, ok.

How about we try this:

Create something that we can diagnose. :slight_smile:

1 Like

If you don’t mind, i’ll do it tomorow cause it’s complicated to include only important parts and i can’t do it rn(

1 Like

I thought that this might be overwhelming for room IDs, so I decided to add a variable to the URL. It might not be the most secure approach, but I don’t believe it will be maliciously used. But thanks a lot anyways, it might be usefull for someone else!

1 Like