New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass HTTP request arguments to autoload_server and pull_session #5992

Closed
anniea opened this Issue Mar 14, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@anniea

anniea commented Mar 14, 2017

#4858 makes it possible to pass HTTP requests arguments to app documents. However, the functions autoload_server and pull_session do not allow for sending request parameters, because they already add a query string to the URL.

For instance, autoload_server adds /autoload.js?bokeh-autoload-element=...&bokeh-session-id=... to the end of the URL. If I add the query string ?N=10 to the end of my app_path, then the URL will result in ?N=10/autoload.js?bokeh-autoload-element=...&bokeh-session-id=... which is not a valid URL.

It would be great if there was a way to pass request arguments to these functions so that they automatically add it to the URL in a correct way.

@kevinsa5

This comment has been minimized.

Contributor

kevinsa5 commented Mar 17, 2017

I ran into this wall also. My use-case is having flask call autoload_server, and I wanted it to simply forward any request parameters through to bokeh. It then used the return script variable as an input to a flask template. Here is my workaround, if you simply must be able to do this before the feature is added:

@app.route('/amped')
def amped():
    script = autoload_server(model = None, app_path="/amped")
    # this is pretty hacky -- flask can't pass its GET request parameters directly, so we have to do it this way
    # `script` is a string that looks like this (the first character is a newline):
    """
<script
    src="http://localhost:5006/amped/autoload.js?bokeh-autoload-element=6b813263-05df-45a5-bd91-e25c5e53c020"
    id="6b813263-05df-45a5-bd91-e25c5e53c020"
    data-bokeh-model-id=""
    data-bokeh-doc-id=""
></script>
"""
    # so to add on the necessary parameters, we have to insert them manually.  hopefully we won't need to urlencode anything.
    # note that request.args = a MultiDict, so be careful of duplicate params
    # http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.MultiDict
    
    script_list = script.split("\n")
    script_list[2] = script_list[2][:-1]
    for key in request.args:
        script_list[2] = script_list[2] + "&{}={}".format(key, request.args[key])
    script_list[2] = script_list[2] + '"'
    script = "\n".join(script_list)
    return render_template("amped.html", script = script)

This goes in and manually edits the script tag returned by autoload_server. It doesn't url-encode any of the request arguments, so beware! In my specific case, this is acceptable, but in general it may not be. Hopefully this is useful to other people with this problem.

@kevinsa5

This comment has been minimized.

Contributor

kevinsa5 commented Mar 18, 2017

I've looked at the source code for autoload_server and it seems like this can be implemented quite easily, at least for GET request parameters. Just allowing an optional argument of a list of (key, value) tuples and then having something like this near the bottom of the function:

for key, value in request_parameters:
    src_path = src_path + "&{}={}".format(key, value)

would be most of the additions. URL-encoding the keys and values would be good too. I would be happy to do the work for this, but I have no experience with contributing to other people's repos on github. If someone could point me to an explanation of the process of making a PR, adding code tests, documentation, etc, I'd love to contribute this to Bokeh!

@jxramos

This comment has been minimized.

jxramos commented Jun 6, 2017

Nice moves @kevinsa5 , I see what you're doing, modifying the src url tucked into the script tag's string. I too would like to see the source updated on autoload_server to permit pushing parameters down into the Bokeh documents.

Unpacking my simple passed string "?paramName=someParam" went like this...

def modify_doc(doc) :
    args      = doc.session_context.request.arguments
    paramName = str( args['paramName'][0].decode('utf-8') )

@bryevdv bryevdv added this to the 0.12.7 milestone Jun 6, 2017

@bryevdv

This comment has been minimized.

Member

bryevdv commented Jun 6, 2017

Too late for 0.12.6 but I have scheduled for the following milestone.

thomas-lab pushed a commit to thomas-lab/bokeh that referenced this issue Jun 8, 2017

thomas-lab pushed a commit to thomas-lab/bokeh that referenced this issue Jun 8, 2017

thomas-lab pushed a commit to thomas-lab/bokeh that referenced this issue Jun 8, 2017

thomas-lab pushed a commit to thomas-lab/bokeh that referenced this issue Jun 15, 2017

thomas-lab pushed a commit to thomas-lab/bokeh that referenced this issue Jun 16, 2017

bdonkey added a commit to bdonkey/bokeh that referenced this issue Jun 17, 2017

Merge branch 'master' of github.com:bdonkey/bokeh into ssdev
ss: after a merge to master from upstream
* 'master' of github.com:bdonkey/bokeh: (134 commits)
  'Updating for version 0.12.7dev1'
  'Updating for version 0.12.7dev1'
  Pass HTTP arguments to autoload_server (bokeh#5992) (bokeh#6415)
  Restore workaround to properly trigger changes in source.data from data_tables (bokeh#6483)
  Start rewriting bokehjs in TypeScript (bokeh#6482)
  Bryanv/6467 noarch (bokeh#6469)
  Canavandl/bokeh png command (bokeh#6471)
  Embed exclude resource files (bokeh#6363)
  'Updating for version 0.12.6'
  'Updating for version 0.12.6'
  'Updating for version 0.12.6'
  'Updating for version 0.12.6'
  'Updating for version 0.12.6rc5'
  'Updating for version 0.12.6rc5'
  protect against exceptions when running inline code (bokeh#6449)
  bump required bkcharts version to 0.2 (bokeh#6445)
  General 0.12.6 examples and docs tasks (bokeh#6408)
  update unemployment svg
  bump canvas2svg version number for image smoothing fix
  Move js_on_change() connections to connect_signals() (bokeh#6420)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment