示例#1
0
def test_get_screenshot_as_png_with_unicode_unminified():
    layout = Div(text="유니 코드 지원을위한 작은 테스트")

    driver = create_webdriver()
    try:
        png = bie.get_screenshot_as_png(layout, driver=driver, resources=Resources(mode="inline", minified=False))
    finally:
        # Have to manually clean up the driver session
        terminate_webdriver(driver)
    assert len(png.tobytes()) > 0
示例#2
0
def test_get_screenshot_as_png_with_unicode_unminified():
    layout = Div(text="유니 코드 지원을위한 작은 테스트")

    driver = create_webdriver()
    try:
        png = bie.get_screenshot_as_png(layout,
                                        driver=driver,
                                        resources=Resources(mode="inline",
                                                            minified=False))
    finally:
        # Have to manually clean up the driver session
        terminate_webdriver(driver)
    assert len(png.tobytes()) > 0
示例#3
0
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()
示例#4
0
文件: save.py 项目: bstadlbauer/panel
def save_png(model, filename):
    """
    Saves a bokeh model to png

    Arguments
    ---------
    model: bokeh.model.Model
      Model to save to png
    filename: str
      Filename to save to
    """
    if not state.webdriver:
        state.webdriver = create_webdriver()

    webdriver = state.webdriver
    export_png(model, filename, webdriver=webdriver)
示例#5
0
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
示例#6
0
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)
示例#7
0
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()
示例#8
0
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
示例#9
0
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)
示例#10
0
    def _figure_data(self, plot, fmt, doc=None, as_script=False, **kwargs):
        """
        Given a plot instance, an output format and an optional bokeh
        document, return the corresponding data. If as_script is True,
        the content will be split in an HTML and a JS component.
        """
        model = plot.state
        if doc is None:
            doc = plot.document
        else:
            plot.document = doc

        for m in model.references():
            m._document = None

        doc.theme = self.theme
        doc.add_root(model)

        # Bokeh raises warnings about duplicate tools and empty subplots
        # but at the holoviews level these are not issues
        logger = logging.getLogger(bokeh.core.validation.check.__file__)
        logger.disabled = True

        if fmt == 'gif':
            from bokeh.io.export import get_screenshot_as_png, create_webdriver
            webdriver = create_webdriver()

            nframes = len(plot)
            frames = []
            for i in range(nframes):
                plot.update(i)
                img = get_screenshot_as_png(plot.state, webdriver)
                frames.append(img)
            webdriver.close()

            bio = BytesIO()
            duration = (1. / self.fps) * 1000
            frames[0].save(bio,
                           format='GIF',
                           append_images=frames[1:],
                           save_all=True,
                           duration=duration,
                           loop=0)
            bio.seek(0)
            data = bio.read()
        elif fmt == 'png':
            from bokeh.io.export import get_screenshot_as_png
            img = get_screenshot_as_png(plot.state, None)
            imgByteArr = BytesIO()
            img.save(imgByteArr, format='PNG')
            data = imgByteArr.getvalue()
        else:
            div = render_mimebundle(plot.state, doc, plot.comm)[0]['text/html']

        if as_script and fmt in ['png', 'gif']:
            b64 = base64.b64encode(data).decode("utf-8")
            (mime_type, tag) = MIME_TYPES[fmt], HTML_TAGS[fmt]
            src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64)
            div = tag.format(src=src, mime_type=mime_type, css='')

        plot.document = doc
        if as_script:
            return div
        else:
            return data
示例#11
0
def webdriver():
    driver = create_webdriver()
    yield driver
    terminate_webdriver(driver)