Beispiel #1
0
 def test_file_html_handles_js_only_resources(self, mock_warn, test_plot):
     js_resources = JSResources(mode="relative", components=["bokeh"])
     template = Template("<head>{{ bokeh_js }}</head><body></body>")
     output = bes.file_html(test_plot, (js_resources, None),
                            "title",
                            template=template)
     html = "<head>%s</head><body></body>" % js_resources.render_js()
     assert output == html
def test_file_html_handles_js_only_resources():
    js_resources = JSResources(mode="relative", components=["bokeh"])
    template = Template("<head>{{ bokeh_js }}</head><body></body>")
    output = embed.file_html(_embed_test_plot, (js_resources, None),
                             "title",
                             template=template)
    html = encode_utf8("<head>%s</head><body></body>" %
                       js_resources.render_js())
    assert output == html
Beispiel #3
0
def test_file_html_handles_js_only_resources():
    js_resources = JSResources(mode="relative")
    template = Template("<head>{{ bokeh_js }}</head><body></body>")
    output = embed.file_html(_embed_test_plot,
                             None,
                             "title",
                             template=template,
                             js_resources=js_resources)
    html = "<head>%s</head><body></body>" % js_resources.use_widgets(
        False).render_js()
    assert output == html
Beispiel #4
0
 def test_file_html_provides_warning_if_no_css(self, mock_warn: MagicMock,
                                               test_plot: figure) -> None:
     js_resources = JSResources()
     bes.file_html(test_plot, (js_resources, None), "title")
     mock_warn.assert_called_once_with(
         'No Bokeh CSS Resources provided to template. If required you will need to provide them manually.'
     )
Beispiel #5
0
def test_file_html_provides_warning_if_both_resources_and_js_provided(
        mock_warn):
    js_resources = JSResources()
    embed.file_html(_embed_test_plot, CDN, "title", js_resources=js_resources)
    mock_warn.assert_called_once_with(
        'Both resources and js_resources provided. resources will override js_resources.'
    )
Beispiel #6
0
    def __init__(self, contents, **kwargs):
        """
        Writes out the content as raw text or HTML.

        :param contents: Bokeh plotting figure.
        :param kwargs: Optional styling arguments. The `style` keyword argument has special
                       meaning in that it allows styling to be grouped as one argument.
                       It is also useful in case a styling parameter name clashes with a standard
                       block parameter.
        """
        self.resource_deps = [
            JScript(script_string=s, name='bokeh_js')
            for s in JSResources().js_raw
        ]
        self.resource_deps += [
            Css(css_string=s, name='bokeh_css') for s in CSSResources().css_raw
        ]

        super(BokehPlotBlock, self).__init__(**kwargs)

        if not isinstance(contents, BokehFigure):
            raise ValueError(
                "Expected bokeh.plotting.figure.Figure type but got %s",
                type(contents))

        script, div = components(contents)
        self._contents = script + div
Beispiel #7
0
def test_file_html_handles_js_only_resources():
    js_resources = JSResources()
    template = Template("<head>{{ bokeh_js }}</head><body></body>")
    output = embed.file_html(_embed_test_plot,
                             None,
                             "title",
                             template=template,
                             js_resources=js_resources)
    rendered_js = JS_RESOURCES.render(js_raw=js_resources.js_raw)
    assert output == "<head>%s</head><body></body>" % rendered_js
Beispiel #8
0
def save_script_tags(open_file):
    # This loads more JS files than is strictly necessary. We really only
    # need the main bokeh file and the widgets file. But it's not yet clear
    # that the gain in loading time is worth the extra complexity of weeding
    # out the other files.
    for f in JSResources(mode='cdn').js_files:
        open_file.write(f'<script type="text/javascript" src="{f}" '
                        'crossorigin="anonymous"></script>\n')

    open_file.write('<script type="text/javascript"> \n'
                    '    Bokeh.set_log_level("info"); \n'
                    '</script>\n')
def test_file_html_provides_warning_if_no_css(mock_warn):
    js_resources = JSResources()
    embed.file_html(_embed_test_plot, (js_resources, None), "title")
    mock_warn.assert_called_once_with(
        'No Bokeh CSS Resources provided to template. If required you will need to provide them manually.'
    )
Beispiel #10
0
    var year = slider.value,
        sources = %s,
        new_source_data = sources[year].data;
    renderer_source.data = new_source_data;
    text_source.data = {'year': [String(year)]};
""" % js_source_array

callback = CustomJS(args=sources, code=code)
slider = Slider(start=years[0], end=years[-1], value=1, step=1, title="Year", callback=callback, name='testy')
callback.args["renderer_source"] = renderer_source
callback.args["slider"] = slider
callback.args["text_source"] = text_source


# Stick the plot and the slider together
layout = column(plot, slider)

# Open our custom template
with open('gapminder_template.jinja', 'r') as f:
    template = Template(f.read())

# Use inline resources, render the html and open
js_resources = JSResources(mode='inline')
title = "Bokeh - Gapminder Bubble Plot"
html = file_html(layout, resources=(js_resources, None), title=title, template=template)

output_file = 'gapminder.html'
with io.open(output_file, mode='w', encoding='utf-8') as f:
    f.write(html)
view(output_file)
Beispiel #11
0
 def test_file_html_handles_js_only_resources(self, mock_warn, test_plot):
     js_resources = JSResources(mode="relative", components=["bokeh"])
     template = Template("<head>{{ bokeh_js }}</head><body></body>")
     output = bes.file_html(test_plot, (js_resources, None), "title", template=template)
     html = encode_utf8("<head>%s</head><body></body>" % js_resources.render_js())
     assert output == html
Beispiel #12
0
def test_file_html_handles_js_only_resources():
    js_resources = JSResources(mode="relative", components=["bokeh"])
    template = Template("<head>{{ bokeh_js }}</head><body></body>")
    output = embed.file_html(_embed_test_plot, (js_resources, None), "title", template=template)
    html = "<head>%s</head><body></body>" % js_resources.render_js()
    assert output == html
Beispiel #13
0
def main(arguments):

    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('details', help="details of filtering results")
    parser.add_argument('hits', help="output of annotate_hits.py")
    parser.add_argument(
        'annotations',
        help="extra annotations for type strains and filter_outliers details")
    parser.add_argument('taxonomy', help="taxonomy in csv format")
    parser.add_argument('type_strains',
                        help='text file of type strain versions')
    parser.add_argument('--param',
                        action='append',
                        default=[],
                        help='colon separated index.html information')
    parser.add_argument('--index-template',
                        default='data/index.jinja',
                        help='location for index.html template [%(default)s]')
    parser.add_argument('--plot-template',
                        default='data/plot.jinja',
                        help='location for the plot template [%(default)s]')
    parser.add_argument('--log-in',
                        help="output of 'deenurp filter_is_outs --log'",
                        type=argparse.FileType('r'))

    parser.add_argument('-i',
                        '--plot-index',
                        default='index.html',
                        help=('name of plot index file, assumed to '
                              'have the same parent dir as plot_dir'))
    parser.add_argument('-d',
                        '--plot-dir',
                        default='plot_details',
                        help=('output directory for plots; index file '
                              'will be named $plot_dir.html'))
    parser.add_argument('--plot-map',
                        metavar='CSV',
                        type=argparse.FileType('w'),
                        help='tax_id,outfile')
    parser.add_argument('-N', type=int, help='max number of taxa to read')

    args = parser.parse_args(arguments)

    plot_dir = args.plot_dir
    plot_dir_name = os.path.basename(plot_dir)
    plot_index = args.plot_index
    json_data_file = 'index.json'

    try:
        os.makedirs(plot_dir)
    except OSError:
        pass

    if args.log_in:
        deenurp_args = {}
        for line in args.log_in:
            spl = line.split('args: ')
            if spl[-1].startswith('{'):
                deenurp_args = json.loads(spl[-1])
                break

    taxonomy = pd.read_csv(args.taxonomy,
                           usecols=['tax_id', 'species', 'tax_name'],
                           dtype={
                               'species': str,
                               'tax_id': str
                           })
    taxonomy = taxonomy.set_index('tax_id')

    species = taxonomy[['species']].join(taxonomy['tax_name'],
                                         on='species',
                                         how='inner')
    species = species.drop_duplicates().set_index('species')

    hits = pd.read_table(args.hits,
                         header=None,
                         dtype={
                             'seqname': str,
                             'hit': str,
                             'pct_id': float
                         },
                         na_filter=False,
                         names=['seqname', 'hit', 'pct_id'],
                         usecols=['seqname', 'hit', 'pct_id'])
    hits = hits.set_index('seqname')

    annos = pd.read_csv(
        args.annotations,
        usecols=['seqname', 'tax_id', 'description', 'version'],
        dtype=str).set_index('seqname')
    types = set(t.strip() for t in open(args.type_strains) if t)
    annos['is_type'] = annos['version'].apply(lambda x: x in types)

    details = pd.read_csv(args.details,
                          usecols=[
                              'seqname', 'tax_id', 'centroid', 'cluster',
                              'dist', 'is_out', 'x', 'y'
                          ],
                          dtype={
                              'seqname': str,
                              'tax_id': str,
                              'centroid': str,
                              'is_out': bool,
                              'cluster': str,
                              'dist': float,
                              'x': float,
                              'y': float
                          })

    details = details.set_index('seqname')
    details = details.join(taxonomy['species'], on='tax_id')  # get species_id
    details = details.join(annos[['description', 'version', 'is_type']])

    hits = hits.join(annos[['tax_id', 'version']], on='hit')  # get tax_id
    hits = hits.join(taxonomy['species'], on='tax_id')  # get species_id
    hits = hits.join(species, on='species')  # get species tax name
    hits = hits.rename(
        columns={
            'species': 'hit_id',
            'tax_name': 'type strain hit',
            'version': 'hit_version'
        })
    hits = hits.drop('tax_id', axis=1)  # we only care about the species_id

    details = details.join(species, on='species')  # get species tax name
    details = details.join(hits)
    details = details.reset_index()  # move seqname back as a column

    # output file purposes
    details.loc[:, 'species_name'] = details['tax_name']

    # make the hit_name link back to an ncbi url to the actual record
    url = ('<a href="https://www.ncbi.nlm.nih.gov/nuccore/{version}">'
           '<div title="{hit}">{hit}</div></a>')

    def hit_ncbi_link(row):
        if pd.isnull(row['type strain hit']):
            tag = ''
        else:
            tag = url.format(**{
                'version': row['hit_version'],
                'hit': row['type strain hit']
            })
        return tag

    details.loc[:, 'type strain hit'] = details.apply(hit_ncbi_link, axis=1)

    details['outlier'] = details['is_out'].apply(lambda x: 'yes' if x else '')
    details['type strain'] = details['is_type'].apply(lambda x: 'yes'
                                                      if x else '')
    # to retain sorting and use with NumberFormatter
    details['identity'] = details['pct_id'] / 100
    details['dist_from_cent'] = details['dist'] * 100

    # define table layout for index page
    table = gviz_api.DataTable([('tax_id', 'string'), ('species', 'string'),
                                ('records', 'number'),
                                ('clustered', 'boolean'),
                                ('is_outs', 'number'), ('pct out', 'number')])

    if args.plot_map:
        map_out = csv.writer(args.plot_map)
        map_out.writerow(['tax_id', 'html'])

    table_data = []
    by_species = details.groupby(by=['species', 'species_name'], sort=False)
    by_species = itertools.islice(by_species, args.N)
    for (species_id, species_name), species in by_species:
        if species.x.isnull().all():
            continue

        species = species.copy().sort_values(by='dist')
        label = '_'.join(species_name.lower().split())

        step_plt, pca_plt, tab = paired_plots(
            species,
            title='{} - {}'.format(species_id, species_name),
            text_cols=[
                'seqname', 'description', 'type strain', 'dist', 'outlier',
                'type strain hit', 'identity'
            ])

        html = file_html(gridplot([[step_plt, pca_plt], [tab]]),
                         (JSResources(mode='cdn'), CSSResources(mode='cdn')),
                         title='{} - {}'.format(species_id, species_name),
                         template=jinja2.Template(
                             open(args.plot_template, 'r').read()))
        filename = '{}.html'.format(label)
        with open(os.path.join(args.plot_dir, filename), 'w') as out:
            out.write(html)

        if args.plot_map:
            map_out.writerow([species_id, filename])

        n_out = int(sum(species['is_out']))
        pct_out = (100.0 * n_out) / len(species)
        clustered = not (species.cluster == -1).all()

        table_data.append([
            species_id,
            '<a href="{}/{}">{}</a>'.format(plot_dir_name, filename,
                                            species_name),
            len(species), clustered, n_out if clustered else None,
            (pct_out, '%.01f%%' % pct_out) if clustered else None
        ])

    with open(os.path.join(plot_dir, json_data_file), 'w') as fout:
        table.AppendData(table_data)
        fout.write(table.ToJSon())

    # plot index should refer to json data according to its relative
    # path, assuming that this file is in same directory as plot_dir
    with open(plot_index, 'w') as fout:
        index = jinja2.Template(open(args.index_template).read())
        fout.write(
            index.render(json_data=os.path.join(plot_dir_name, json_data_file),
                         deenurp_args=deenurp_args,
                         params=dict(p.split(':') for p in args.param)))
import requests
from sqlalchemy import create_engine, String, Integer
from sqlalchemy.sql import text, bindparam
from google.cloud import bigquery
import geopandas as gpd
from shapely.geometry import shape
from datetime import datetime
from cartoframes.viz import Layer, Map, color_category_style, popup_element

from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.resources import CSSResources, JSResources
from shapely import wkt

bokeh_css = CSSResources(mode="cdn", version="2.2.3", minified=True)
bokeh_js = JSResources(mode="cdn", version="2.2.3", minified=True)

application = Flask(__name__, template_folder="templates")

bqclient = bigquery.Client.from_service_account_json(
    "Musa509-Lab5-42148b13cb70.json")

#load credentials from a file
with open("pg-credentials.json", "r") as f_in:
    pg_creds = json.load(f_in)

# mapbox
with open("mapbox_token.json", "r") as mb_token:
    MAPBOX_TOKEN = json.load(mb_token)["token"]

# load credentials from JSON file
Beispiel #15
0
# Import the markers from the file

import_marker = process_PAsearch.get_markerpos(stub_loc['stub_dir'][0])

df_MRK = pd.DataFrame.from_dict(import_marker, orient='index')
df_MRK.columns = ['StgX', 'StgY']  # add the column names

# ------------ Create the plots --------------
# Open our custom template
with open('PB_template.jinja', 'r') as f:
    template = Template(f.read())

# Use inline resources, render the html and open
bokehlayout = create_bokehplot.makelayout(df_EDAX, df_MRK, img_list)
title = 'Particle Search Results'
js_resources = JSResources(mode='cdn')
css_resources = CSSResources(mode='cdn')
html = file_html(bokehlayout,
                 resources=(js_resources, css_resources),
                 title=title,
                 template=template,
                 template_variables=sample_info)

script, div = components(bokehlayout)
"""html = template.render(js_resources=js_resources,
                       css_resources=css_resources,
                       div=div)"""

output_file = directory + '.html'

with open(directory + '/' + output_file, mode='w', encoding='utf-8') as f:
Beispiel #16
0
def test_file_html_handles_js_only_resources():
    js_resources = JSResources(mode="relative")
    template = Template("<head>{{ bokeh_js }}</head><body></body>")
    output = embed.file_html(_embed_test_plot, None, "title", template=template, js_resources=js_resources)
    html = "<head>%s</head><body></body>" % js_resources.use_widgets(False).render_js()
    assert output == html