def get_event_plot(): """ Connects the frontend to the Bokeh Server by pulling and returning a sesssion which generates Bokeh plots displaying all the events for a given run Returns: str: A script tag that embeds content from a specific existing session on a Bokeh server. """ if request.is_json: token = request.args.get("token") req = request.get_json() run_id = None user = None try: run_id = req["run_id"] user = req["user"] except: return make_response(jsonify({"error": "Bad Request"}), 400) if not authenticate(user, token): return make_response(jsonify({"error": "Unauthorized API request"}), 403) print("RUN ID IS: ", run_id) threading.Thread(target=cache_events_request, args=[run_id]).start() session_id = generate_session_id() my_sessions.insert_one({session_id: run_id}) script = server_session(url=BOKEH_SERVER_URL, session_id=session_id) # use the script in the rendered page return script else: return make_response(jsonify({"error": "Bad Request"}), 400)
def bokeh_plugin(analysis_name): session_url = "http://localhost:5006/" + analysis_name # generate a script to load the customized session script = server_session(session_id='test_session_id', url=session_url) # use the script in the rendered page return render_template("analysis_tabbed_bokeh.html", script=script, template="Flask")
def index(): with pull_session(url="http://localhost:5006/") as session: # generate a script to load the customized session script = server_session(session_id=session.id, url='http://localhost:5006') # use the script in the rendered page return render_template("index.html", script=script, template="Flask")
def decoders(): with pull_session(url=decoders_url) as session: script = server_session(session_id=session.id, url=decoders_url) return render_template("decoders.html", script=script, template="Flask")
def data_advance(request): session = pull_session(url="http://139.196.84.157:8080/offline1") script = server_session(None, url="http://139.196.84.157:8080/offline1", session_id=session.id) return render(request, 'data_advance.html', {'script': script})
def bkapp_page(): # script = server_document('http://localhost:5006/myapp') # return render_template("embed.html", script=script, template="Flask") name = request.args.get("app") if 'app' in request.args else 'myapp' with pull_session(session_id='1234567890', url="http://localhost:5006/" + name) as session: session.request_server_info() # update or customize that session # session.document.roots[0].children[1].title.text = "Special Sliders For A Specific User!" # generate a script to load the customized session cur_doc = session.document button_bokeh = Button(label="Custom Style: Bokeh Button", css_classes=['custom_button_bokeh']) cur_doc.add_root(button_bokeh) script = server_session(session_id=session.id, url='http://localhost:5006/' + name) # use the script in the rendered page template_file = "embed.html" if name == 'dash': template_file = "index.html" # page_source = server_html_page_for_session(session, resources=None, title='测试', template=template_file, template_variables=None) # page_source = page_source.replace('root.Bokeh.embed.embed_items(docs_json, render_items)', "root.Bokeh.embed.embed_items(docs_json, render_items, '/dash', 'http://localhost:5006/dash')" ) page_source = render_template(template_file, script=script, template="Flask") return page_source
def plojo_help(): root = urlparse(request.base_url).hostname script = server_session(url='http://' + root + ':5006/plojo_help', session_id=generate_session_id()) return render_template('apps/simuojo.html', script=script, title='Plojo Help')
def sliders_view(request): # Define bokeh endpoint url bokeh_server_url = "%sbokehproxy/sliders" % (request.build_absolute_uri( location='/')) # Generate bokeh session token so user can access plot, this is done for all logged in users per the @login_required decorator # ensuring only logged in users can view plots # Using newer bokeh server_session method vs. deprecated bokeh.embed.autoload_server # Note: session_id.generate_session_id() relies on the presence of BOKEH_SECRET_KEY defined in settings.py via an OS variable server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) # Tip: More elaborate permission checks can be made using Django's user system, to generate (or not) bokeh session accesss tokens: # if user.is_authenticated() and user.has_perm("bokehdash.change_plot"): # server_session(None, session_id=....) # else: # HttpResponseRedirect("You can't see this plot") # Tip2: More elaborate permissions checks can also be made with other method decorators @user_passes_test, @permission_required # (besides @login_reqired) # Proceed with context and response context = { "graphname": "Sliders", "server_script": server_script, } return render(request, 'bokehdash/bokeh_server.html', context)
def getDataLoaderModule(request, editMode=True): template = None endPoint = 'studio-pipeline/dataloader/' # on récupère l'url de l'application du Board du serveur Bokeh bokeh_server_url = "%s" % (request.build_absolute_uri( location='/')) + endPoint session_id = request.COOKIES.get('session_id') if session_id == None: session_id = token.generate_session_id() headers = request.headers headers = dict(headers) if headers else {} headers['session'] = request.COOKIES.get('crsftoken') server_script = server_session(None, session_id=session_id, url=bokeh_server_url, headers=headers) # Création du context pour le template Jinja context = {"dataloader": server_script} template = render(request, 'data/data_loader.html', context) return template
def interlingua(): with pull_session(url=interlingua_url) as session: script = server_session(session_id=session.id, url=interlingua_url) return render_template("interlingua.html", script=script, template="Flask")
def preview_script(self, request: Request, pk: int = None) -> Response: arguments = {"scan_id": str(pk)} destination_id = request.GET.get("elementId", "bk-plot") with pull_session(url=BOKEH_URL, arguments=arguments) as session: html = server_session(session_id=session.id, url=BOKEH_URL) script = fix_bokeh_script(html, destination_id=destination_id) return HttpResponse(script, content_type="text/javascript")
def bokeh_js_script(relative_urls=True): #session = pull_session(self.address) #session.document.template_variables['data_path'] = self.data_path s_id = session_id.generate_session_id(secret_key=BOKEH_KEY, signed=True) return server_session(None, session_id=s_id, url=BOKEH_ADDRESS, relative_urls=relative_urls, resources='default')
def hello(): session = pull_session(session_id=None, url='http://localhost:5006/bokeh_plot') script = server_session(session_id=session.id, url='http://localhost:5006/bokeh_plot') return render_template('htmlbits.html', script=script, js_resources=js_resources, css_resources=css_resources)
def bkapp_page(self): with pull_session(url="http://localhost:8080/sliders") as session: # update or customize that session session.document.roots[0].children[1].title.text = "Special Sliders For A Specific User!" # generate a script to load the customized session script = server_session(session_id=session.id, url='http://localhost:8080/sliders') # use the script in the rendered page return render_template("embed.html", script=script, template="Flask")
def data(request): session1 = pull_session(url="http://139.196.84.157:8080/realtime1") script1 = server_session(None, url="http://139.196.84.157:8080/realtime1", session_id=session1.id) session2 = pull_session(url="http://139.196.84.157:8080/realtime2") script2 = server_session(None, url="http://139.196.84.157:8080/realtime2", session_id=session2.id) session3 = pull_session(url="http://139.196.84.157:8080/realtime3") script3 = server_session(None, url="http://139.196.84.157:8080/realtime3", session_id=session3.id) return render(request, 'data.html', { 'script1': script1, 'script2': script2, 'script3': script3 })
def total_deaths_since_first(request): bokeh_server_url = "%sbokehproxy/covid-total-deaths-since-first" % ( request.build_absolute_uri(location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "server_script": server_script, } return render(request, 'charts/total-deaths-since-first.html', context)
def interactive(request): #bokeh_script=autoload_server(None, url="http://localhost:5006/random-generator") server_script = server_session(None, session_id=session_id.generate_session_id(), url="http://localhost:5006/slider") context = { "graphname": "Sliders", "server_script": server_script, } return render(request, 'bokehapp/interactive.html', context)
def daily_confirmed_vs_time(request): bokeh_server_url = "%sbokehproxy/covid-daily-confirmed-cases" % ( request.build_absolute_uri(location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "server_script": server_script, } return render(request, 'charts/daily-confirmed-cases.html', context)
def confirmed_against_time_bar(request): bokeh_server_url = "%sbokehproxy/covid-confirmed-against-time-bar" % ( request.build_absolute_uri(location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "server_script": server_script, } return render(request, 'charts/confirmed-against-time-bar.html', context)
def confirmed_uk_map_plot(request): bokeh_server_url = "%sbokehproxy/covid-confirmed-uk-map-plot" % ( request.build_absolute_uri(location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "server_script": server_script, } return render(request, 'charts/confirmed-uk-map-plot.html', context)
def plot_script(self, request, *args, **kwargs): destination_id = request.GET.get("id", "bk-plot") with pull_session(url=BOKEH_URL) as session: script = server_session(session_id=session.id, url=BOKEH_URL) soup = BeautifulSoup(script, features="lxml") element = soup(["script"])[0] random_id = element.attrs["id"] script = element.contents[0] script = script.replace(random_id, destination_id) return HttpResponse(script, content_type="text/javascript")
def country_stats(request): bokeh_server_url = "%sbokehproxy/covid-country-stats" % ( request.build_absolute_uri(location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "server_script": server_script, } return render(request, 'charts/country-stats.html', context)
def deaths_vs_time(request): bokeh_server_url = "%sbokehproxy/covid-deaths-against-time" % ( request.build_absolute_uri(location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "server_script": server_script, } return render(request, 'charts/deaths-against-time.html', context)
def index(): index = "home" with pull_session(url=BOKEH_URL) as session: # generate a script to load the customized session script = server_session(session_id=session.id, url=BOKEH_URL) return render_template("index.html", script=script, template="Flask", index=index)
def Test(request): bokeh_server_url = "%sbokehproxy/Test" % (request.build_absolute_uri( location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "graphname": "Test", "server_script": server_script, } return render(request, 'charts/Test.html', context)
def histogram_view(request): bokeh_server_url = "%sbokehproxy/selection_histogram" % ( request.build_absolute_uri(location='/')) server_script = server_session(None, session_id=session_id.generate_session_id(), url=bokeh_server_url) context = { "graphname": "Selection Histogram", "server_script": server_script, } return render(request, 'bokehdash/bokeh_server.html', context)
def index(): # pull new session from running bokeh server with pull_session(url=app_url) as session: # update or customise that session session.document.roots[0].title.text = "Random Generator" #Generate a script to load the customised session script = server_session(session_id=session.id, url=app_url) return render_template("index.html", script = script, template = "Flask")
def choropleth(request): #pull the session from the actively running bokeh app app_url = "http://localhost:5600/bokeh_choropleth" with pull_session(url=app_url) as session: server_script = server_session(session_id=session.id, url=app_url) script = server_script form = AnalysisForm return render(request, 'analysis/analysis.html', {'form': form, 'div': script})
def bkapp_page(request): with pull_session(url="http://localhost:5006/Interactive") as session: # update or customize that session session.document.roots[0].children[ 1].title.text = "Special Sliders For A Specific User!" # generate a script to load the customized session script = server_session(session_id=session.id, url="http://localhost:5006/Interactive") # use the script in the rendered page return render(request, "embed.html", dict(script=script))
def wik(): """App route for wik application.""" if 'auth' in session.keys(): if session['auth'] == 1: script1 = server_session(url=f'{BOKEH_URL}', session_id=generate_session_id()) return render_template("app.html", script1=script1, relative_urls=True) return redirect(f"{WIK_URL}/login")
def bkapp_page(): # pull a new session from aunning Bokeh server with pull_session(url=app_url) as session: # update or customize that session session.document.roots[0].title.text = "Special Plot Title For A Specific User!" # generate a script to load the customized session script = server_session(session_id=session.id, url=app_url) # use the script in the rendered page return render_template("embed.html", script=script, template="Flask")
location_select.on_change('value', on_location_change) controls = row(children=[year_select, location_select]) layout = column(children=[controls, pyramid(), population()]) return layout layout = create_layout() update_data() html = """ <html> <head></head> <body> %s </body> </html> """ % server_session(layout, session_id=session.id, relative_urls=False) with io.open("widget.html", mode='w+', encoding='utf-8') as f: f.write(html) print(__doc__) document.add_root(layout) if __name__ == "__main__": print("\npress ctrl-C to exit") session.loop_until_closed()