def test_get_screenshot_as_png_with_glyph(): layout = Plot(x_range=Range1d(0, 1), y_range=Range1d(0, 1), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, min_border=2, border_fill_color="blue", border_fill_alpha=1) glyph = Rect(x="x", y="y", width=2, height=2, fill_color="red", line_color="red") source = ColumnDataSource(data=dict(x=[0.5], y=[0.5])) layout.add_glyph(source, glyph) png = bie.get_screenshot_as_png(layout) assert png.size == (20, 20) # count 256 red pixels in center area (400 - 20*4 - 16*4) data = png.tobytes() count = 0 for x in range(400): if data[x * 4:x * 4 + 4] == b"\xff\x00\x00\xff": count += 1 assert count == 256 assert len(data) == 1600
def test_show_doesn_not_adds_obj_to_curdoc(m): curstate().reset() assert curstate().document.roots == [] p = Plot() bis.show(p) assert curstate().document.roots == [] p = Plot() bis.show(p) assert curstate().document.roots == []
def test__show_with_state_with_notebook(mock_get_browser_controller, mock__show_file_with_state, mock_run_notebook_hook): mock_get_browser_controller.return_value = "controller" s = State() p = Plot() s.output_notebook() bis._show_with_state(p, s, "browser", "new") assert s.notebook_type == "jupyter" assert mock_run_notebook_hook.call_count == 1 assert mock_run_notebook_hook.call_args[0] == ("jupyter", "doc", p, s, False) assert mock_run_notebook_hook.call_args[1] == {} assert mock__show_file_with_state.call_count == 0 s.output_file("foo.html") bis._show_with_state(p, s, "browser", "new") assert s.notebook_type == "jupyter" assert mock_run_notebook_hook.call_count == 2 assert mock_run_notebook_hook.call_args[0] == ("jupyter", "doc", p, s, False) assert mock_run_notebook_hook.call_args[1] == {} assert mock__show_file_with_state.call_count == 1 assert mock__show_file_with_state.call_args[0] == (p, s, "new", "controller") assert mock__show_file_with_state.call_args[1] == {}
def test_zeppelin_with_notebook_handle(self): load_zeppelin_args = (Resources(), True, True, 1000, 'zeppelin') io.output_notebook(*load_zeppelin_args) with pytest.raises(Exception) as ex: p = Plot() io.show(p, notebook_handle=True) assert "Zeppelin doesn't support notebook_handle." == str(ex.value)
def test_layout_html_on_parent_first(): p = Plot(x_range=Range1d(), y_range=Range1d()) layout = row(p) bie.get_layout_html(layout) bie.get_layout_html(p, height=100, width=100)
def test_get_svgs_with_svg_present(webdriver): def fix_ids(svg): svg = re.sub(r'id="\w{12}"', 'id="X"', svg) svg = re.sub(r'url\(#\w{12}\)', 'url(#X)', svg) return svg layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, border_fill_color=None, background_fill_color="red", output_backend="svg") svg0 = fix_ids(bie.get_svgs(layout, driver=webdriver)[0]) svg1 = fix_ids(bie.get_svgs(layout, driver=webdriver)[0]) svg2 = ( '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' 'width="20" height="20" style="width: 20px; height: 20px;">' '<defs/>' '<g>' '<g transform="scale(1,1) translate(0.5,0.5)">' '<rect fill="#FFFFFF" stroke="none" x="0" y="0" width="20" height="20"/>' '<rect fill="red" stroke="none" x="5" y="5" width="10" height="10"/>' '<g/>' '</g>' '</g>' '</svg>') assert svg0 == svg2 assert svg1 == svg2
def test_show_doesnt_duplicate_if_already_there(m): io._state.document.clear() p = Plot() io.show(p) assert io._state.document.roots == [p] io.show(p) assert io._state.document.roots == [p]
def test_show_doesnt_duplicate_if_already_there(m): curstate().reset() assert curstate().document.roots == [] p = Plot() bis.show(p) assert curstate().document.roots == [p] bis.show(p) assert curstate().document.roots == [p]
def test__save_layout_html_resets_plot_dims(): initial_height, initial_width = 200, 250 layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=initial_height, plot_width=initial_width) io._save_layout_html(layout, height=100, width=100) assert layout.plot_height == initial_height assert layout.plot_width == initial_width
def test_get_svgs_no_svg_present(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None) svgs = bie.get_svgs(layout) assert svgs == []
def test_show_with_explicit_args(mock__show_with_state): curstate().reset() kwargs = dict(browser="browser", new="new", notebook_handle=True) p = Plot() bis.show(p, **kwargs) assert mock__show_with_state.call_count == 1 assert mock__show_with_state.call_args[0] == (p, curstate(), "browser", "new") assert mock__show_with_state.call_args[1] == {'notebook_handle': True} assert p in curdoc().roots
def test_show_with_default_args(mock__show_with_state): curstate().reset() default_kwargs = dict(browser=None, new="tab", notebook_handle=False) p = Plot() bis.show(p, **default_kwargs) assert mock__show_with_state.call_count == 1 assert mock__show_with_state.call_args[0] == (p, curstate(), None, "tab") assert mock__show_with_state.call_args[1] == {'notebook_handle': False} assert p in curdoc().roots
def test__get_screenshot_as_png(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) png = io._get_screenshot_as_png(layout) assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00"*1600).encode()
def test__get_svgs_with_svg_present(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, border_fill_color=None, background_fill_color=None, output_backend="svg") svgs = io._get_svgs(layout) assert svgs[0] == ('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' 'width="20" height="20" style="width: 20px; height: 20px;"><defs/><g><g/><g transform="scale(1,1) ' 'translate(0.5,0.5)"><rect fill="#FFFFFF" stroke="none" x="0" y="0" width="20" height="20"/><g/><g/><g/><g/></g></g></svg>')
def test_writing_in_cow_context_persists(self): with app.test_request_context("/"): t = self.transaction('test1') t.load() t.clientdoc.add(Plot(title='plot2')) t.save() t = self.transaction('test1') t.load() assert len(t.clientdoc.context.children) == 2 assert t.clientdoc.context.children[1].title == 'plot2'
def test_save_helper_method(self, mock_file_html, mock_io_open): obj = Plot() filename, resources, title = io._get_save_args(io._state, "filename", "resources", "title") io._save_helper(obj, filename, resources, title) self._check_func_called(mock_file_html, (obj, resources), {"title": "title"}) self._check_func_called(mock_io_open, (filename,), {"mode":"w", "encoding":"utf-8"})
def setUp(self): super(TransactionManagerTestCase, self).setUp() self.user = new_user(bokeh_app.servermodel_storage, "test1", "password") self.server_docobj = _makedoc(bokeh_app.servermodel_storage, self.user, 'testdoc1') #create 2 views original_doc = bokeh_app.backbone_storage.get_document( self.server_docobj.docid) plot1 = Plot(title='plot1') original_doc.add(plot1) bokeh_app.backbone_storage.store_document(original_doc)
def test__save_helper(mock_file_html, mock_io_open): obj = Plot() filename, resources, title = bis._get_save_args(curstate(), "filename", "resources", "title") bis._save_helper(obj, filename, resources, title) assert mock_file_html.call_count == 1 assert mock_file_html.call_args[0] == (obj, resources) assert mock_file_html.call_args[1] == {"title": "title"} assert mock_io_open.call_count == 1 assert mock_io_open.call_args[0] == (filename,) assert mock_io_open.call_args[1] == {"mode":"w", "encoding":"utf-8"}
def test_save_layout_html_resets_plot_dims(): initial_height, initial_width = 200, 250 layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=initial_height, plot_width=initial_width) with NamedTemporaryFile() as tmp: bie.save_layout_html(layout, tmp.name, height=100, width=100) assert layout.plot_height == initial_height assert layout.plot_width == initial_width
def test__save_helper(mock_file_html, mock_io_open): obj = Plot() filename, resources, title = bis._get_save_args(curstate(), "filename", "resources", "title") bis._save_helper(obj, filename, resources, title, None) assert mock_file_html.call_count == 1 assert mock_file_html.call_args[0] == (obj, resources) assert mock_file_html.call_args[1] == dict(title="title", template=None) assert mock_io_open.call_count == 1 assert mock_io_open.call_args[0] == (filename, ) assert mock_io_open.call_args[1] == dict(mode="w", encoding="utf-8")
def test_writing_in_cow_context_does_not_modify_original(self): with app.test_request_context("/"): t = self.transaction('test1') t.load() t.clientdoc.add(Plot(title='plot2')) t.save() t = self.transaction('test1') t.load() assert len(t.clientdoc.context.children) == 2 t = self.transaction(None) t.load() assert len(t.clientdoc.context.children) == 1
def test_get_screenshot_as_png_large_plot(webdriver): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=800, plot_width=800, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) bie.get_screenshot_as_png(layout, driver=webdriver) # LC: Although the window size doesn't match the plot dimensions (unclear # why), the window resize allows for the whole plot to be captured assert webdriver.get_window_size() == {'width': 1366, 'height': 768}
def test_obj_arg_is_layoutdom(self, mock_standalone_html_page, mock_io_open): obj = Plot() doc = Document() doc.add_root(obj) filename, resources, title = io._get_save_args(io._state, "filename", "resources", "title") io._save_helper(obj, filename, resources, title, True) self._check_func_called(mock_standalone_html_page, (doc, resources, title), {}) self._check_func_called(mock_io_open, (filename, ), { "mode": "w", "encoding": "utf-8" })
def test__get_svgs_with_svg_present_with_driver(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, border_fill_color=None, background_fill_color=None, output_backend="svg") driver = webdriver.PhantomJS(service_log_path=os.path.devnull) svgs = io._get_svgs(layout) # Have to manually clean up the driver session driver.quit() assert svgs[0] == ('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' 'width="20" height="20" style="width: 20px; height: 20px;"><defs/><g><g/><g transform="scale(1,1) ' 'translate(0.5,0.5)"><rect fill="#FFFFFF" stroke="none" x="0" y="0" width="20" height="20"/><g/><g/><g/><g/></g></g></svg>')
def test__get_screenshot_as_png_with_driver(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = webdriver.PhantomJS(service_log_path=os.path.devnull) png = io._get_screenshot_as_png(layout, driver=driver) # Have to manually clean up the driver session driver.quit() assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00"*1600).encode()
def test__get_screenshot_as_png_large_plot(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=800, plot_width=800, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = webdriver.PhantomJS(service_log_path=os.path.devnull) assert driver.get_window_size() == {'width': 400, 'height': 300} io._get_screenshot_as_png(layout, driver=driver) # LC: Although the window size doesn't match the plot dimensions (unclear # why), the window resize allows for the whole plot to be captured assert driver.get_window_size() == {'width': 1366, 'height': 768} # Have to manually clean up the driver session driver.quit()
def test_get_svgs_with_svg_present(): def fix_ids(svg): svg = re.sub(r'id="\w{12}"', 'id="X"', svg) svg = re.sub(r'url\(#\w{12}\)', 'url(#X)', svg) return svg layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, border_fill_color=None, background_fill_color="red", output_backend="svg") svg0 = fix_ids(bie.get_svgs(layout)[0]) driver = create_webdriver() try: svg1 = fix_ids(bie.get_svgs(layout)[0]) finally: terminate_webdriver( driver) # Have to manually clean up the driver session svg2 = ( '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' 'width="20" height="20" style="width: 20px; height: 20px;">' '<defs>' '<clipPath id="X"><path fill="none" stroke="none" d=" M 5 5 L 15 5 L 15 15 L 5 15 L 5 5 Z"/></clipPath>' '<clipPath id="X"><path fill="none" stroke="none" d=" M 5 5 L 15 5 L 15 15 L 5 15 L 5 5 Z"/></clipPath>' '</defs>' '<g>' '<g transform="scale(1,1) translate(0.5,0.5)">' '<rect fill="#FFFFFF" stroke="none" x="0" y="0" width="20" height="20"/>' '<rect fill="red" stroke="none" x="5" y="5" width="10" height="10"/>' '<g/>' '<g clip-path="url(#X)"><g/></g>' '<g clip-path="url(#X)"><g/></g>' '<g/>' '</g>' '</g>' '</svg>') assert svg0 == svg2 assert svg1 == svg2
def test_get_screenshot_as_png_with_driver(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = create_webdriver() try: png = bie.get_screenshot_as_png(layout, driver=driver) finally: terminate_webdriver(driver) assert png.size == (20, 20) # a 20x20px image of transparent pixels assert png.tobytes() == ("\x00" * 1600).encode()
def test_get_screenshot_as_png_large_plot(): layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=800, plot_width=800, toolbar_location=None, outline_line_color=None, background_fill_color=None, border_fill_color=None) driver = create_webdriver() try: assert driver.get_window_size() == {'width': 400, 'height': 300} bie.get_screenshot_as_png(layout, driver=driver) # LC: Although the window size doesn't match the plot dimensions (unclear # why), the window resize allows for the whole plot to be captured assert driver.get_window_size() == {'width': 1366, 'height': 768} finally: # Have to manually clean up the driver session terminate_webdriver(driver)
def _test_children_removed_from_root(layout_generator, children=None): component = Plot() io.curdoc().add_root(component if children is None else children[0][0]) layout_generator(component if children is None else children) assert component not in io.curdoc().roots io.curdoc().clear()