Passing arguments from Flask to Bokeh Server using server_session

Hi All,

I have encountered a problem when trying to pass arguments from Flask into Bokeh Server.

If I am running my Bokeh Server independently I can pass my arguments in no problem, as I can print out my arguments and all the graphs work correctly

#data.py

args = curdoc().session_context.request.arguments

print('Arguments are: ', args)

However when I add in my Flask application

#app.py

@app.route(“/”)

def index():

get_id = request.args.get(“ID”)

ID = {‘ID’: get_id}

print(ID)

with pull_session(url=‘http://localhost:5006/data’) as session:

bokeh_script = server_session(session_id=session.id, url=“http:/localhost:5006/data”, arguments=ID)

return render_template(‘index.html’, bokeh_script=bokeh_script)

and run the following

bokeh serve --allow-websocket-origin=localhost:5000 data.py --num-procs=1

``

the page loads however data.py prints "Arguments are: {} " however I know my arguments are being accepted in Flask as ID is printing a dictionary successfully.

Here is the documentation for server_session, it explicitly says that arguments bokeh.embed — Bokeh 3.3.2 Documentation

Thanks in advance!

Hi,

This probably needs to be clarified in the docs, or maybe better, made to raise an error. The code in the app script is executed when pull_session is called, so by the time you call server_session, it is simply too late. There's nothing that could be done about that.

There are two possibilities:

* If you only need to set this ID and not do any other configuration of the Bokeh document, you don't need to use pull_session at all. Just call server_session or server_document with the arguments parameter.

* Otherwise if you do need to use pull_session, you might be able to bake the request args into the URL, i.e. something like:

  pull_session(url='http://localhost:5006/data?foo=bar'\)

If that does not work it will require an issue and investigation/work so see if it's possible to add.

It would be nice to make it clear (in docs and in code) that arguments cannot work when server_session is used with an existing session. Would you be interested in helping the project by working up a small PR to do that with some guidance?

Thanks,

Bryan

···

On Aug 5, 2018, at 22:50, Ol1BoT <[email protected]> wrote:

Hi All,

I have encountered a problem when trying to pass arguments from Flask into Bokeh Server.

If I am running my Bokeh Server independently I can pass my arguments in no problem, as I can print out my arguments and all the graphs work correctly

#data.py
args = curdoc().session_context.request.arguments
print('Arguments are: ', args)

However when I add in my Flask application
..

#app.py
@app.route("/")
def index():
   get_id = request.args.get("ID")
   ID = {'ID': get_id}
   print(ID)
   with pull_session(url='http://localhost:5006/data&#39;\) as session:
       bokeh_script = server_session(session_id=session.id, url="http:/localhost:5006/data", arguments=ID)
       return render_template('index.html', bokeh_script=bokeh_script)

and run the following

>bokeh serve --allow-websocket-origin=localhost:5000 data.py --num-procs=1

the page loads however data.py prints "Arguments are: {} " however I know my arguments are being accepted in Flask as ID is printing a dictionary successfully.

Here is the documentation for server_session, it explicitly says that arguments bokeh.embed — Bokeh 3.3.2 Documentation

Thanks in advance!

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/5bb3955d-be3a-4f50-898e-e64beddf85f2%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Hi Bryan,

Many thanks for your reply - I took your advice and used server_document and removed the pull_session:

@app.route(“/”)

def index():

get_id = request.args.get(“ID”)

ID = {‘ID’: str(get_id)}

print(ID)

bokeh_script = server_document(url=‘http://localhost:5006/data’, arguments=ID)

return render_template(‘index.html’, bokeh_script=bokeh_script)

In reply to your question, while I would be willing to help, I am a python hobbyist at best and my overall grasp of both Python and Bokeh is pretty sub-par - I would just make a fool of myself; an example being I don’t really understand the difference between pull_session and server_document.

Thanks again,

Ollie

···

On Tuesday, 7 August 2018 01:01:32 UTC+10, Bryan Van de ven wrote:

Hi,

This probably needs to be clarified in the docs, or maybe better, made to raise an error. The code in the app script is executed when pull_session is called, so by the time you call server_session, it is simply too late. There’s nothing that could be done about that.

There are two possibilities:

  • If you only need to set this ID and not do any other configuration of the Bokeh document, you don’t need to use pull_session at all. Just call server_session or server_document with the arguments parameter.

  • Otherwise if you do need to use pull_session, you might be able to bake the request args into the URL, i.e. something like:

      pull_session(url='[http://localhost:5006/data?foo=bar](http://localhost:5006/data?foo=bar)')
    

If that does not work it will require an issue and investigation/work so see if it’s possible to add.

It would be nice to make it clear (in docs and in code) that arguments cannot work when server_session is used with an existing session. Would you be interested in helping the project by working up a small PR to do that with some guidance?

Thanks,

Bryan

On Aug 5, 2018, at 22:50, Ol1BoT [email protected] wrote:

Hi All,

I have encountered a problem when trying to pass arguments from Flask into Bokeh Server.

If I am running my Bokeh Server independently I can pass my arguments in no problem, as I can print out my arguments and all the graphs work correctly

#data.py

args = curdoc().session_context.request.arguments

print('Arguments are: ', args)

However when I add in my Flask application

#app.py

@app.route(“/”)

def index():

get_id = request.args.get(“ID”)

ID = {‘ID’: get_id}

print(ID)

with pull_session(url=‘http://localhost:5006/data’) as session:

   bokeh_script = server_session(session_id=[session.id](http://session.id), url="http:/localhost:5006/data", arguments=ID)
   return render_template('index.html', bokeh_script=bokeh_script)

and run the following

bokeh serve --allow-websocket-origin=localhost:5000 data.py --num-procs=1

the page loads however data.py prints "Arguments are: {} " however I know my arguments are being accepted in Flask as ID is printing a dictionary successfully.

Here is the documentation for server_session, it explicitly says that arguments https://bokeh.pydata.org/en/latest/docs/reference/embed.html#bokeh.embed.server_session

Thanks in advance!


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/5bb3955d-be3a-4f50-898e-e64beddf85f2%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Hi,

I am glad that that worked. Looking more closely, I think the "arguments" parameter just needs to be removed altogether from server_session, since it can never work properly. Can you make a GitHub issue with the information from this thread so that we can keep track of this issue?

  Issues · bokeh/bokeh · GitHub

Thanks,

Bryan

···

On Aug 6, 2018, at 22:06, [email protected] wrote:

Hi Bryan,

Many thanks for your reply - I took your advice and used server_document and removed the pull_session:

@app.route("/")
def index():
    get_id = request.args.get("ID")
    ID = {'ID': str(get_id)}
    print(ID)
    bokeh_script = server_document(url='http://localhost:5006/data&#39;, arguments=ID)
    return render_template('index.html', bokeh_script=bokeh_script)

In reply to your question, while I would be willing to help, I am a python hobbyist at best and my overall grasp of both Python and Bokeh is pretty sub-par - I would just make a fool of myself; an example being I don't really understand the difference between pull_session and server_document.

Thanks again,

Ollie

On Tuesday, 7 August 2018 01:01:32 UTC+10, Bryan Van de ven wrote:
Hi,

This probably needs to be clarified in the docs, or maybe better, made to raise an error. The code in the app script is executed when pull_session is called, so by the time you call server_session, it is simply too late. There's nothing that could be done about that.

There are two possibilities:

* If you only need to set this ID and not do any other configuration of the Bokeh document, you don't need to use pull_session at all. Just call server_session or server_document with the arguments parameter.

* Otherwise if you do need to use pull_session, you might be able to bake the request args into the URL, i.e. something like:

        pull_session(url='http://localhost:5006/data?foo=bar&#39;\)

If that does not work it will require an issue and investigation/work so see if it's possible to add.

It would be nice to make it clear (in docs and in code) that arguments cannot work when server_session is used with an existing session. Would you be interested in helping the project by working up a small PR to do that with some guidance?

Thanks,

Bryan

> On Aug 5, 2018, at 22:50, Ol1BoT <[email protected]> wrote:
>
> Hi All,
>
> I have encountered a problem when trying to pass arguments from Flask into Bokeh Server.
>
> If I am running my Bokeh Server independently I can pass my arguments in no problem, as I can print out my arguments and all the graphs work correctly
>
> #data.py
> args = curdoc().session_context.request.arguments
> print('Arguments are: ', args)
>
> However when I add in my Flask application
> ..
>
> #app.py
> @app.route("/")
> def index():
> get_id = request.args.get("ID")
> ID = {'ID': get_id}
> print(ID)
> with pull_session(url='http://localhost:5006/data&#39;\) as session:
> bokeh_script = server_session(session_id=session.id, url="http:/localhost:5006/data", arguments=ID)
> return render_template('index.html', bokeh_script=bokeh_script)
>
> and run the following
>
> >bokeh serve --allow-websocket-origin=localhost:5000 data.py --num-procs=1
>
> the page loads however data.py prints "Arguments are: {} " however I know my arguments are being accepted in Flask as ID is printing a dictionary successfully.
>
> Here is the documentation for server_session, it explicitly says that arguments bokeh.embed — Bokeh 3.3.2 Documentation
>
> Thanks in advance!
>
> --
> You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bokeh+un...@continuum.io.
> To post to this group, send email to bo...@continuum.io.
> To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/5bb3955d-be3a-4f50-898e-e64beddf85f2%40continuum.io\.
> For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/eb489a74-c7fa-46bc-a68d-18f0e0cee717%40continuum.io\.
For more options, visit https://groups.google.com/a/continuum.io/d/optout\.

Just a quick message to say that passing the argument parameter worked perfectly. I am serving panel apps using Flask, and the query parameters passed in the URL become on time accessible with pn.state.session_args (panel style) as well as curdoc.session_context.request.arguments (bokeh style).
It doesn’t work as well if arguments is not passed (there are some hickups I won’t detail here).

Might be because Bokeh evolved a lot since 2018 :slight_smile: .