def test_launch_server_with_stream(self): obj = Curve([]) stream = RangeXY(source=obj) launched = [] def modify_doc(doc): bokeh_renderer(obj, doc=doc) launched.append(True) server.stop() handler = FunctionHandler(modify_doc) app = Application(handler) server = Server({'/': app}, port=0) server.start() url = "http://localhost:" + str(server.port) + "/" pull_session(session_id='Test', url=url, io_loop=server.io_loop) self.assertTrue(len(launched)==1) cb = bokeh_renderer.last_plot.callbacks[0] self.assertIsInstance(cb, RangeXYCallback) self.assertEqual(cb.streams, [stream]) x_range = bokeh_renderer.last_plot.handles['x_range'] self.assertIn(cb.on_change, x_range._callbacks['start']) self.assertIn(cb.on_change, x_range._callbacks['end']) y_range = bokeh_renderer.last_plot.handles['y_range'] self.assertIn(cb.on_change, y_range._callbacks['start']) self.assertIn(cb.on_change, y_range._callbacks['end'])
def test__check_error_404(self, ManagedServerLoop) -> None: application = Application() with ManagedServerLoop(application) as server: with pytest.raises(IOError): pull_session(session_id='test__check_error_404', url=url(server) + 'file_not_found', io_loop=server.io_loop)
def test_log_stats(): application = Application() with ManagedServerLoop(application) as server: server._tornado.log_stats() session1 = pull_session(session_id="session1", url=url(server), io_loop=server.io_loop) session2 = pull_session(session_id="session2", url=url(server), io_loop=server.io_loop) server._tornado.log_stats() session1.close() session2.close() server._tornado.log_stats()
def create_sessions(slugs, titles): app1_slug, app2_slug = slugs apps = { app1_slug: Markdown('First app'), app2_slug: Markdown('Second app') } server = serve(apps, port=5008, title=titles, show=False, start=False) servers.append(server) session1 = pull_session(url=f"http://localhost:{server.port:d}/app1", io_loop=server.io_loop) session2 = pull_session(url=f"http://localhost:{server.port:d}/app2", io_loop=server.io_loop) return session1, session2
def test_launch_simple_server(self): obj = Curve([]) launched = [] def modify_doc(doc): bokeh_renderer(obj, doc=doc) launched.append(True) server.stop() handler = FunctionHandler(modify_doc) app = Application(handler) server = Server({'/': app}, port=0) server.start() url = "http://localhost:" + str(server.port) + "/" pull_session(session_id='Test', url=url, io_loop=server.io_loop) self.assertTrue(len(launched)==1)
def test_log_stats(ManagedServerLoop) -> None: application = Application() with ManagedServerLoop(application) as server: server._tornado._log_stats() session1 = pull_session(session_id='session1', url=url(server), io_loop=server.io_loop) session2 = pull_session(session_id='session2', url=url(server), io_loop=server.io_loop) server._tornado._log_stats() session1.close() session2.close() server._tornado._log_stats()
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 run(self): setproctitle.setproctitle(f"bokeh_session_{self.streamer_id}") with pull_session(url="http://localhost:5006/stream-debugger") as session: doc = session.document source = ColumnDataSource({"x": [], "y": [], "color": []}) def update(): new = { "x": [random.random()], "y": [random.random()], "color": [random.choice(["red", "blue", "green"])], } source.stream(new) doc.add_periodic_callback(update, 100) fig = figure( title="Streaming Circle Plot!", sizing_mode="scale_width", x_range=[0, 1], y_range=[0, 1], ) fig.circle(source=source, x="x", y="y", color="color", size=10) print(f"http://localhost:5006/stream-debugger?session_id={session.id}") doc.title = "Now with live updating!" doc.add_root(fig) session = push_session( doc, session_id=session.id, url="http://localhost:5006/stream-debugger" ) session.show() while True: print(self.port) time.sleep(1)
def test_pull_large_document(self, ManagedServerLoop) -> None: application = Application() def add_roots(doc): import numpy as np rows, cols = (40000, 100) columns=['x'+str(i) for i in range(cols)] a = np.random.randn(cols, rows) source = ColumnDataSource(data=dict(zip(columns, a))) doc.add_root(source) handler = FunctionHandler(add_roots) application.add(handler) with ManagedServerLoop(application) as server: client_session = pull_session(session_id='test_pull_document', url=url(server), io_loop=server.io_loop, max_message_size=50000000) assert len(client_session.document.roots) == 1 server_session = server.get_session('/', client_session.id) assert len(server_session.document.roots) == 1 results = {} for r in server_session.document.roots: if hasattr(r, 'data'): results['data'] = r.data assert len(list(results['data'].keys())) == 100 assert all(len(x) == 40000 for x in results['data'].values()) client_session.close() client_session._loop_until_closed() assert not client_session.connected
def population_plot(title="Default", year="2015", location="World"): """About page.""" login_form = LoginForm(request.form) id = bkutil.session_id.generate_session_id(signed=True) session = bkclient.pull_session(session_id=id, url=bokeh_app_url('population')) print("Session id is: {}".format(session.id)) app_param = session.document.get_model_by_name("app_param") # as soon we change data from app_param the plot is updated app_param.data['year'] = [year] app_param.data['location'] = [location] html = """ <div id="population-plot"> {F} </div> """.format(F=bkembed.autoload_server( model=None, session_id=session.id, url=bokeh_app_url('population'))) plot = {"plot1": html} df = load_population() years = [str(x) for x in sorted(df.Year.unique())] locations = sorted(df.Location.unique()) return render_template('public/population_plot.html', form=login_form, plot=plot, subnav=PLOTS_SUBNAV(), session_id=id, years=years, default_year=year, locations=locations, default_location=location)
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 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 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 bkapp_command(): with pull_session(session_id='1234567890', url="http://localhost:5006/myapp") as session: session.request_server_info() cur_doc = session.document button = cur_doc.get_model_by_name('button_text') cur_doc.remove_root(button) import json from bokeh.events import Event data = '{"event_name": "document_update", "event_values" : {"x": 10, "y": 20, "sx": 200, "sy": 37}}' json.loads(data) event = json.loads(data, object_hook=Event.decode_json) event = json.loads(data) cur_doc.apply_json_event(event) from bokeh.protocol import Protocol # from bokeh.events import DocumentUpdate # document_update_event = DocumentUpdate() # protocol = Protocol() # message = protocol.create("PATCH-DOC", [document_update_event]) # message.apply_to_document(cur_doc) #session._connection.send_message(message) from bokeh.document.events import MessageSentEvent document_patched_event = MessageSentEvent( document=cur_doc, msg_type='append_dataset', msg_data='append_dataset_data') protocol = Protocol() message = protocol.create("PATCH-DOC", [document_patched_event]) # message.apply_to_document(cur_doc) session._connection.send_message(message) return ''
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 population_class_plot(title="Default", year=0): """About page.""" form = LoginForm(request.form) id = bkutil.session_id.generate_session_id(signed=True) session = bkclient.pull_session(session_id=id, url=bokeh_app_url('population_class')) print("Session info:") #print (session.request_server_info()) print("Session id is: {}".format(session.id)) mypy = session.document.get_model_by_name('bk-pyramid') mypop = session.document.get_model_by_name('bk-population') mymodel = session.document.get_model_by_name('bk-classdemo') print("Model bk-pyramid") print(mypy) print("Model bk-population") print(mypop) print("Model bk-classdemo") print(mymodel) html = """ <div id="population-class-plot"> {F} </div> """.format( F=bkembed.autoload_server(model=None, session_id=session.id, url=bokeh_app_url('population_class'))) plot = {} plot["plot1"] = html if year > 0: mymodel.change_year(year) return render_template('public/population_plot.html', form=form, plot=plot, subnav=PLOTS_SUBNAV())
def test_pull_document(self, ManagedServerLoop: MSL) -> None: application = Application() def add_roots(doc: Document): doc.add_root(AnotherModelInTestClientServer(bar=43)) doc.add_root( SomeModelInTestClientServer(foo=42, data=bytes( [0x00, 0x01, 0xFE, 0xFF]))) handler = FunctionHandler(add_roots) application.add(handler) with ManagedServerLoop(application) as server: client_session = pull_session(session_id=ID("test_pull_document"), url=url(server), io_loop=server.io_loop) assert len(client_session.document.roots) == 2 server_session = server.get_session('/', client_session.id) assert len(server_session.document.roots) == 2 results = {} for r in server_session.document.roots: if hasattr(r, 'foo'): results['foo'] = r.foo if hasattr(r, 'bar'): results['bar'] = r.bar assert results['foo'] == 42 assert results['bar'] == 43 client_session.close() client_session._loop_until_closed() assert not client_session.connected
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 test_pull_document(self): application = Application() def add_roots(doc): doc.add_root(AnotherModelInTestClientServer(bar=43)) doc.add_root(SomeModelInTestClientServer(foo=42)) handler = FunctionHandler(add_roots) application.add(handler) with ManagedServerLoop(application) as server: client_session = pull_session(session_id='test_pull_document', url=server.ws_url, io_loop=server.io_loop) assert len(client_session.document.roots) == 2 server_session = server.get_session('/', client_session.id) assert len(server_session.document.roots) == 2 results = {} for r in server_session.document.roots: if hasattr(r, 'foo'): results['foo'] = r.foo if hasattr(r, 'bar'): results['bar'] = r.bar assert results['foo'] == 42 assert results['bar'] == 43 client_session.close() client_session.loop_until_closed() assert not client_session.connected
def test_pull_document(self): application = Application() def add_roots(doc): doc.add_root(AnotherModelInTestClientServer(bar=43)) doc.add_root(SomeModelInTestClientServer(foo=42)) handler = FunctionHandler(add_roots) application.add(handler) with ManagedServerLoop(application) as server: client_session = pull_session(session_id='test_pull_document', url=url(server), io_loop=server.io_loop) assert len(client_session.document.roots) == 2 server_session = server.get_session('/', client_session.id) assert len(server_session.document.roots) == 2 results = {} for r in server_session.document.roots: if hasattr(r, 'foo'): results['foo'] = r.foo if hasattr(r, 'bar'): results['bar'] = r.bar assert results['foo'] == 42 assert results['bar'] == 43 client_session.close() client_session.loop_until_closed(suppress_warning=True) assert not client_session.connected
def _launcher(self, obj, threaded=False, io_loop=None): if io_loop: io_loop.make_current() launched = [] def modify_doc(doc): bokeh_renderer(obj, doc=doc) launched.append(True) handler = FunctionHandler(modify_doc) app = Application(handler) server = Server({'/': app}, port=0, io_loop=io_loop) server.start() self._port = server.port self._server = server if threaded: server.io_loop.add_callback(self._loaded.set) thread = threading.current_thread() state._thread_id = thread.ident if thread else None io_loop.start() else: url = "http://localhost:" + str(server.port) + "/" session = pull_session(session_id='Test', url=url, io_loop=server.io_loop) self.assertTrue(len(launched) == 1) return session, server return None, server
def get_bokeh_script(self, suffix): assert hasattr(self, 'object') bokeh_session = pull_session(session_id=None, url='http://localhost:5006/%s/' % suffix) # We want to make this less cumbersome see https://github.com/bokeh/bokeh/issues/3349 user_source = bokeh_session.document.get_model_by_name('user_pk_source') user_source.data = dict(user_pk=[self.object.pk]) script = autoload_server(None, app_path='/%s' % suffix, session_id=bokeh_session.id) return script
def interactive(): session = pull_session(url="http://localhost:5006/mushroom_classifier") bokeh_script = autoload_server( None, url="http://localhost:5006/mushroom_classifier", session_id=session.id) return render_template("mushroomclassifier.html", bokeh_script=bokeh_script)
def test_launch_server_with_complex_plot(self): dmap = DynamicMap(lambda x_range, y_range: Curve([]), streams=[RangeXY()]) overlay = dmap * HLine(0) static = Polygons([]) * Path([]) * Curve([]) layout = overlay + static launched = [] def modify_doc(doc): bokeh_renderer(layout, doc=doc) launched.append(True) server.stop() handler = FunctionHandler(modify_doc) app = Application(handler) server = Server({'/': app}, port=0) server.start() url = "http://localhost:" + str(server.port) + "/" pull_session(session_id='Test', url=url, io_loop=server.io_loop) self.assertTrue(len(launched)==1)
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 interactive(): url = 'http://104.131.82.22:5006' session = pull_session(url=url, app_path='/mushroom_classifier') bokeh_script = autoload_server(None, app_path='/mushroom_classifier', session_id=session.id, url=url) return render_template("mushroomclassifier.html", bokeh_script=bokeh_script)
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 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 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 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 index(): # pull a new session from a running Bokeh server bokeh_server_url = 'http://localhost:5006/aion-analytics' bokeh_session = pull_session(url=bokeh_server_url) script = "{}?bokeh-session-id={}".format(bokeh_server_url, bokeh_session.id) logger.warning("bokeh url:%s", script) return render_template('security/two-factor/templates/index.html', script=script, template="Flask")
def html_server_session(): html = HTML('<h1>Title</h1>') server = serve(html, port=5006, show=False, start=False) session = pull_session(session_id='Test', url="http://localhost:{:d}/".format(server.port), io_loop=server.io_loop) yield html, server, session try: server.stop() except AssertionError: pass # tests may already close this
def markdown_server_session(): html = Markdown('#Title') server = serve(html, port=6001, show=False, start=False) session = pull_session(session_id='Test', url="http://localhost:{:d}/".format(server.port), io_loop=server.io_loop) yield html, server, session try: server.stop() except AssertionError: pass # tests may already close this
def callback(sl, c, lbl): print ''' Entered callback ''' session = pull_session(session_id='labeler') document = session.document document.clear() # record(sl, c, lbl) # sl, c = next(examples) # widget(sl, c) print '''
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")
def update_bokeh_sessions(user_sessions): for us in user_sessions: with closing(pull_session(session_id=us.bokehSessionId, url=bokeh_url)) as session: if len(session.document.roots) == 0: # In this case, the session_id was from a dead session and # calling pull_session caused a new empty session to be # created. So we just delete the UserSession and move on. # It would be nice if there was a more efficient way - where I # could just ask bokeh if session x is a session. us.delete() else: update_time_series_data(user=us.user, session=session)
def update_bokeh_sessions(user_sessions): for us in user_sessions: with closing(pull_session(session_id=us.bokeh_session_id)) as session: if len(session.document.roots) == 0: # In this case, the session_id was from a dead session and # calling pull_session caused a new empty session to be # created. So we just delete the UserSession and move on. # It would be nice if there was a more efficient way - where I # could just ask bokeh if session x is a session. us.delete() else: # Call the appropriate update method based on the document's title if session.document.title == 'individuals': update_individuals_data(user=us.user, session=session) if session.document.title == 'team': update_team_data(user=us.user, session=session) if session.document.title == 'teams': update_teams_data(user=us.user, session=session)
def test_lots_of_concurrent_messages(self): application = Application() def setup_stuff(doc): m1 = AnotherModelInTestClientServer(bar=43, name='m1') m2 = SomeModelInTestClientServer(foo=42, name='m2') m3 = SomeModelInTestClientServer(foo=68, name='m3') doc.add_root(m1) doc.add_root(m2) doc.add_root(m3) def timeout1(): m1.bar += 1 doc.add_timeout_callback(timeout1, 1) def timeout2(): m2.foo +=1 doc.add_timeout_callback(timeout2, 3) def periodic1(): m1.bar += 1 doc.remove_timeout_callback(timeout1) doc.add_timeout_callback(timeout1, m1.bar % 7) doc.add_periodic_callback(periodic1, 3) def periodic2(): m2.foo += 1 doc.remove_timeout_callback(timeout2) doc.add_timeout_callback(timeout2, m2.foo % 7) doc.add_periodic_callback(periodic2, 1) def server_on_change(event): if isinstance(event, ModelChangedEvent) and event.model is m3: return m3.foo += 1 doc.on_change(server_on_change) handler = FunctionHandler(setup_stuff) application.add(handler) # keep_alive_milliseconds=1 sends pings as fast as the OS will let us with ManagedServerLoop(application, keep_alive_milliseconds=1) as server: session = pull_session(session_id='test_lots_of_concurrent_messages', url=url(server), io_loop=server.io_loop) assert session.connected server_session = server.get_session('/', session.id) def client_timeout(): m = session.document.roots[0] m.name = m.name[::-1] session.document.add_timeout_callback(client_timeout, 3) def client_periodic(): m = session.document.roots[1] m.name = m.name[::-1] session.document.remove_timeout_callback(client_timeout) session.document.add_timeout_callback(client_timeout, 3) session.document.add_periodic_callback(client_periodic, 1) result = {} def end_test(): result['connected'] = session.connected result['server_connection_count'] = server_session.connection_count result['server_close_code'] = next(iter(server._tornado._clients))._socket.close_code result['doc'] = session.document.to_json() session.close() # making this longer is more likely to trigger bugs, but it also # makes the test take however much time you put here session.document.add_timeout_callback(end_test, 250) def client_on_change(event): if not isinstance(event, TitleChangedEvent): session.document.title = session.document.title[::-1] session.document.on_change(client_on_change) session.loop_until_closed() assert not session.connected # we should have still been connected at the end, # if we didn't have any crazy protocol errors assert 'connected' in result assert result['connected'] # server should also still have been connected assert result['server_connection_count'] == 1 assert result['server_close_code'] is None
def dask_dashboard(request, dask_scheduler_id, page='status'): dask_object = DaskScheduler.objects.get(id=dask_scheduler_id) name = dask_object.name base_link = dask_object.dashboard # Append http(s) if not exists if 'http' not in base_link: base_link = 'http://' + base_link # Add forward slash if not exist if base_link[-1] != '/': base_link = base_link + '/' # Define url link for each page url_link = base_link + page status_link = reverse('admin:dask_dashboard', kwargs={'page': 'status', 'dask_scheduler_id': dask_scheduler_id}) workers_link = reverse('admin:dask_dashboard', kwargs={'page': 'workers', 'dask_scheduler_id': dask_scheduler_id}) tasks_link = reverse('admin:dask_dashboard', kwargs={'page': 'tasks', 'dask_scheduler_id': dask_scheduler_id}) systems_link = reverse('admin:dask_dashboard', kwargs={'page': 'system', 'dask_scheduler_id': dask_scheduler_id}) profile_link = reverse('admin:dask_dashboard', kwargs={'page': 'profile', 'dask_scheduler_id': dask_scheduler_id}) graph_link = reverse('admin:dask_dashboard', kwargs={'page': 'graph', 'dask_scheduler_id': dask_scheduler_id}) header_link = {"workers_link": workers_link, "tasks_link": tasks_link, "systems_link": systems_link, "profile_link": profile_link, "graph_link": graph_link, "status_link": status_link} if page == 'status': # Get link for each bokeh application url_link_nbytes = base_link + 'individual-nbytes' url_link_nprocessing = base_link + 'individual-nprocessing' url_link_task_stream = base_link + 'individual-task-stream' url_link_progress = base_link + 'individual-progress' # Generate script using bokeh server_document method nbyte_script = server_document(url_link_nbytes) nprocessing_script = server_document(url_link_nprocessing) task_stream_script = server_document(url_link_task_stream) progress_script = server_document(url_link_progress) # get Div ID nbyte_id = re.search('id="(.+?)"><', nbyte_script).group(1) nprocessing_id = re.search('id="(.+?)"><', nprocessing_script).group(1) task_stream_id = re.search('id="(.+?)"><', task_stream_script).group(1) progress_id = re.search('id="(.+?)"><', progress_script).group(1) context = {"the_name": name, "nbyte_script": nbyte_script, "nprocessing_script": nprocessing_script, "task_stream_script": task_stream_script, "progress_script": progress_script, "nbyte_id": nbyte_id, "nprocessing_id": nprocessing_id, "task_stream_id": task_stream_id, "progress_id": progress_id} # add Header link into context context.update(header_link) return render(request, "tethys_compute/dask_scheduler_status.html", context) # For system we have to use session to load because they have many plots inside one application. If we use # use server_document, it will look very ugly when load to our page. elif page == 'system': with pull_session(url=url_link) as session: id_dict = {} scripts, divs = components({session.document.roots[0].title.text: session.document.roots[0], session.document.roots[1].title.text: session.document.roots[1], session.document.roots[2].title.text: session.document.roots[2], session.document.roots[3].title.text: session.document.roots[3]}) # Extract out ID for each graph type for div in divs: id_value = re.search('id="(.+?)"><', divs[div]).group(1) id_dict[div.replace(' ', '_')] = {'id': id_value} context = {"the_script": scripts, "the_divs": id_dict, "the_name": name} # add Header link into context context.update(header_link) # use the script in the rendered page return render(request, "tethys_compute/dask_scheduler_system.html", context) else: script = server_document(url_link) context = {"the_name": name, "the_script": script} # Add header link into context context.update(header_link) if page == 'workers': return render(request, "tethys_compute/dask_scheduler_workers.html", context) elif page == 'tasks': return render(request, "tethys_compute/dask_scheduler_tasks.html", context) elif page == 'profile': return render(request, "tethys_compute/dask_scheduler_profile.html", context) elif page == 'graph': return render(request, "tethys_compute/dask_scheduler_graph.html", context)
def test__lifecycle_hooks(): application = Application() handler = HookTestHandler() application.add(handler) with ManagedServerLoop(application, check_unused_sessions_milliseconds=30) as server: # wait for server callbacks to run before we mix in the # session, this keeps the test deterministic def check_done(): if len(handler.hooks) == 4: server.io_loop.stop() server_load_checker = PeriodicCallback(check_done, 1) server_load_checker.start() server.io_loop.start() server_load_checker.stop() # now we create a session client_session = pull_session(session_id='test__lifecycle_hooks', url=url(server), io_loop=server.io_loop) client_doc = client_session.document assert len(client_doc.roots) == 1 server_session = server.get_session('/', client_session.id) server_doc = server_session.document assert len(server_doc.roots) == 1 # we have to capture these here for examination later, since after # the session is closed, doc.roots will be emptied client_hook_list = client_doc.roots[0] server_hook_list = server_doc.roots[0] client_session.close() # expire the session quickly rather than after the # usual timeout server_session.request_expiration() def on_done(): server.io_loop.stop() server.io_loop.call_later(0.1, on_done) server.io_loop.start() assert handler.hooks == ["server_loaded", "next_tick_server", "timeout_server", "periodic_server", "session_created", "next_tick_session", "modify", "timeout_session", "periodic_session", "session_destroyed", "server_unloaded"] assert handler.load_count == 1 assert handler.unload_count == 1 # this is 3 instead of 6 because locked callbacks on destroyed sessions # are turned into no-ops assert handler.session_creation_async_value == 3 assert client_doc.title == "Modified" assert server_doc.title == "Modified" # only the handler sees the event that adds "session_destroyed" since # the session is shut down at that point. assert client_hook_list.hooks == ["session_created", "modify"] assert server_hook_list.hooks == ["session_created", "modify"]