2016-04-13 9 views
2

私はbokehアプリケーションを作成しており、状態(ウィジェットに影響する)を「共有可能」にしたいと考えています。私の最初の考えは、URLにクエリ文字列を使用することでした。しかし、実際のHTTPリクエストがアプリケーションで利用可能かどうかはわかりません。HTTP要求を使用したBokehプロット状態の制御

例:bokeh serve main.py添えとhttp://localhost:5006/mainでアクセスされる

# main.py 
from bokeh.models.widgets import Select 
from bokeh.plotting import Figure 
from bokeh.io import curdoc, vform 

p = Figure() 
selector = Select(title="Select", options=["1", "2", "3"], value="2") 

p.line([1, selector.value], [1, selector.value]) 

curdoc().add_root(vform(p, selector)) 

http://localhost:5006/main?select=3に移動した場合、元のリクエストにselect=3が含まれていて、デフォルトの2ではなく3になっていることがアプリに分かる方法がありますか?あるいは、私はこれをまったく間違った方法に近づけて、より良い解決策を見逃していますか?

This Q&A関連ですが、ボケサーバーがトルネードに建てられているのですが、今は古くなっていると思います。

+1

HTTPリクエストは、ボケ0.11.1のように現在利用可能ではありませんが、それは可能性があり、様々なアイデアがあちこちで提案されています。この開発の方向性に貢献することに興味がある場合は、ここであなたの考えを共有することをお勧めします:https://github.com/bokeh/bokeh/issues/3349 – bigreddot

+0

ありがとう。私はそこに従い、私が何かを見たらチャイムする。 – TomAugspurger

答えて

0

最終的に私はこれを得ることができました。私は本当に誰もこのコードを使用することをお勧めしますが、それが役に立つ場合には、私は確信していません....

私はFlaskのアプリケーションでBokehアプリケーションをラップしました。基本的な考え方は、フラスコビューでクエリパラメータを解析し、Bokehドキュメントを歩き、クエリパラメータの指示に従ってオブジェクトを更新することです。

私はstate、我々はそのapp.pyで、フラスコ(またはジャンゴ/何でも)アプリをインポートクエリパラメータによって制御可能なウィジェットのリスト

# file: main.py 
# ... 
state = [selector] 
# ... 

が含まれるようにmain.pyを更新しました。

# file: app.py 
import json 
from urllib.parse import urlencode 

from flask import Flask, request, render_template 

from bokeh.client import pull_session 
from bokeh.embed import autoload_server 
app = Flask(__name__) 


@app.route('/') 
def index(): 
    session = pull_session(app_path='/main') 
    doc = session.document 
    update_document(doc, request) # this is the important part 
    script = autoload_server(model=None, 
          app_path='/main', session_id=session.id) 
    return render_template('index.html', script=script, session_id=session.id) 


def update_document(doc, request): 
    ''' 
    Update a Bokeh document according to the query string in ``request`` 

    Parameters 
    ---------- 
    doc: Bokeh.Document 
    request: flask.request 

    Returns 
    ------- 
    None: (Modifies doc inplace) 
    ''' 
    for k, v in request.args.items(): 
     # titles must be unique 
     model = doc.select_one(dict(title=k)) 
     v = parse_query_value(v) 
     model.update(value=v) 


def parse_query_value(v): 
    ''' 
    Parse a single parameter's value from the query string 

    Parameters 
    ---------- 
    v : str 

    Returns 
    ------- 
    parsed : object 
    ''' 
    if v.startswith('['): 
     return json.loads(v.replace("'", '"')) 
    return v 


@app.route('/state/<session_id>') 
def get_state(session_id): 
    ''' 
    Get the current widget state. 

    Parameters 
    ---------- 
    session_id : int 
     id of the connection to the bokeh server from ``session.id``. 
     this should be unique per tab. 
    ''' 
    session = pull_session(app_path='/main', 
          session_id=session_id) 
    doc = session.document 
    params = {} 
    for key in state: 
     params[key] = doc.select_one(dict(title=key)).value 
    return urlencode(params) 


if __name__ == '__main__': 
    app.run() 

そしてindex.html

<html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 

    </head> 
    <body> 
    <h1 class='title'>Title</h1> 

    <div class="plot">{{ script|safe }}</div> 

    </body> 

</html> 
関連する問題