Esempio n. 1
0
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
Esempio n. 2
0
def test_get_svgs_with_svg_present(webdriver: WebDriver) -> None:
    plot = lambda color: Plot(
        x_range=Range1d(),
        y_range=Range1d(),
        height=20,
        width=20,
        toolbar_location=None,
        outline_line_color=None,
        border_fill_color=None,
        background_fill_color=color,
        output_backend="svg",
    )

    layout = row([plot("red"), plot("blue")])

    with silenced(MISSING_RENDERERS):
        svgs0 = bie.get_svgs(layout, driver=webdriver)
        svgs1 = bie.get_svgs(layout, driver=webdriver)

    svgs2 = [
        '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="20" height="20">'
        '<defs/>'
        '<path fill="rgb(255,0,0)" stroke="none" paint-order="stroke" d="M 5.5 5.5 L 15.5 5.5 L 15.5 15.5 L 5.5 15.5 L 5.5 5.5" fill-opacity="1"/>'
        '</svg>',
        '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="20" height="20">'
        '<defs/>'
        '<path fill="rgb(0,0,255)" stroke="none" paint-order="stroke" d="M 5.5 5.5 L 15.5 5.5 L 15.5 15.5 L 5.5 15.5 L 5.5 5.5" fill-opacity="1"/>'
        '</svg>',
    ]

    assert svgs0 == svgs2
    assert svgs1 == svgs2
Esempio n. 3
0
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
Esempio n. 4
0
def test_get_svgs_no_svg_present() -> None:
    layout = Plot(x_range=Range1d(), y_range=Range1d(), plot_height=20, plot_width=20, toolbar_location=None)

    with silenced(MISSING_RENDERERS):
        svgs = bie.get_svgs(layout)

    assert svgs == []
Esempio n. 5
0
def output_svg(fig, driver=None):
    """
    Generates an SVG from the Bokeh figure. Errors in the
    process are logged and an SVG with error text is returned.

    Parameters
    ----------
    fig : bokeh.plotting.Figure
    driver : selenium.webdriver.remote.webdriver.WebDriver, default None
        Web driver to use to render SVG figures. With bokeh<2.0 this
        defaults to trying to use phantomjs.

    Returns
    -------
    svg : str
    """
    fig.output_backend = 'svg'
    try:
        svg = get_svgs(fig, driver=driver)[0]
    except Exception:
        logger.error('Could not generate SVG for figure %s',
                     getattr(fig, 'name', 'unnamed'))
        svg = ('<svg width="100%" height="100%">'
               '<text x="50" y="50" class="alert alert-error">'
               'Unable to generate SVG plot.'
               '</text>'
               '</svg>')
    return svg
Esempio n. 6
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
Esempio n. 7
0
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 == []
Esempio n. 8
0
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 = bie.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>')
Esempio n. 9
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
Esempio n. 10
0
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="red", output_backend="svg")

    svgs = bie.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 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/><g/></g></g></svg>')
Esempio n. 11
0
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 = bie.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_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 = bie.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>'
    )
Esempio n. 13
0
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 == []