Esempio n. 1
0
    def compare(self, other, field='cumtime', count=35):
        left = self._get_timing_data(count, field, field)
        left.name = "left"
        right = other._get_timing_data(count, field, field)
        right.name = "right"

        fig = plt.figure()
        self._show_timing_data(
            left[::-1],
            field,
            ax=fig.add_subplot('131')
        )
        self._show_timing_data(
            right[::-1],
            field,
            ax=fig.add_subplot('133')
        )

        data = pd.concat([left, right], axis=1).fillna(0)
        data['diff'] = data['left'] - data['right']
        data = data.sort('diff', ascending=False)

        # HACK: Make pandas always show an HTML Table repr for this frame.
        data._repr_fits_horizontal_ = lambda *args, **kwargs: True
        display_html(data._repr_html_(), raw=True)
Esempio n. 2
0
    def _ipython_display_(self):
        from IPython.display import display_javascript, display_html
        from IPython.display import Javascript

        jscode = self.js()
        display_javascript(Javascript(jscode, lib=[D3, NVD3], css=[NVD3_CSS]))
        display_html(self.html(), raw=True)
Esempio n. 3
0
 def _ipython_display_(self):
     for name, structure in self.items():
         if structure:
             display_html('<h4>{}</h4>'.format(name), raw=True)
             display_html('<p>{}</p>'.format(
                 structure.__repr__().replace('\n', '<br>').replace(' ', '&nbsp;')
             ), raw=True)
Esempio n. 4
0
    def _ipython_display_(self):
        try:
            column_types_json = json.dumps(self.column_types)
            data_frame_json = self.df_copy.to_json(
                orient='records',
                date_format='iso',
                double_precision=self.precision,
            )

            if self.remote_js:
                cdn_base_url = \
                    "https://cdn.rawgit.com/quantopian/qgrid/ddf33c0efb813cd574f3838f6cf1fd584b733621/qgrid/qgridjs/"
            else:
                cdn_base_url = "/nbextensions/qgridjs"

            raw_html = SLICK_GRID_CSS.format(
                div_id=self.div_id,
                cdn_base_url=cdn_base_url,
            )
            raw_js = SLICK_GRID_JS.format(
                cdn_base_url=cdn_base_url,
                div_id=self.div_id,
                data_frame_json=data_frame_json,
                column_types_json=column_types_json,
            )

            display_html(raw_html, raw=True)
            display_javascript(raw_js, raw=True)
        except Exception as err:
            display_html('ERROR: {}'.format(str(err)), raw=True)
Esempio n. 5
0
    def view(self,
             count,
             sort,
             fields=None,
             show_table=True,
             show_graph=True,
             return_data=False,
             **mpl_kwargs):

        if fields is None:
            fields = self.default_view_fields
        data = self._get_timing_data(count, sort, fields)

        if show_table:
            # HACK: Make pandas always show an HTML Table repr for this frame.
            data._repr_fits_horizontal_ = lambda *args, **kwargs: True
            display_html(data._repr_html_(), raw=True)

        if show_graph:
            self._show_timing_data(
                data.ix[::-1, sort],
                sort,
                **mpl_kwargs
            )

        if return_data:
            return data
Esempio n. 6
0
    def _ipython_display_(self):
        try:
            column_types_json = json.dumps(self.column_types)
            data_frame_json = self.df_copy.to_json(
                orient='records',
                date_format='iso',
                double_precision=self.precision,
            )
            options_json = json.dumps(self.grid_options)

            if self.remote_js:
                cdn_base_url = \
                    "https://cdn.rawgit.com/quantopian/qgrid/72d356cb123fab413dba73ec46616e4916fbd827/qgrid/qgridjs/"
            else:
                cdn_base_url = "/nbextensions/qgridjs"

            raw_html = SLICK_GRID_CSS.format(
                div_id=self.div_id,
                cdn_base_url=cdn_base_url,
            )
            raw_js = SLICK_GRID_JS.format(
                cdn_base_url=cdn_base_url,
                div_id=self.div_id,
                data_frame_json=data_frame_json,
                column_types_json=column_types_json,
                options_json=options_json,
            )

            display_html(raw_html, raw=True)
            display_javascript(raw_js, raw=True)
        except Exception as err:
            display_html('ERROR: {}'.format(str(err)), raw=True)
Esempio n. 7
0
def init():
    display_html("""
<style type="text/css">
    .state {
        width: 110px;
        height: 50px;
        background: pink;
        font-weight: bold;
    }
    .box {
        display: flex;
        justify-content: center;
        align-items: center;
        border: 3px solid grey;
        margin: 5px;
        padding: 5px;
    }
    .tape {
        width: 50px;
        height: 50px;
    }
    .head {
        background: yellow;
        font-weight: bold;
    }
    .configuration {
        display: flex;
        flex-wrap: wrap;
        flex-direction: row;
    }
</style>
""", raw=True)
Esempio n. 8
0
    def load_module(self, fullname):
        """import a notebook as a module"""
        path = find_notebook(fullname, self.path)
        disp = "importing IPython notebook from "
        disp += "<a href='./{}' target='_blank'>{}</a>".format(path, path[:-6])
        display_html(disp, raw=True)
        # print disp

        # load the notebook object
        with io.open(path, "r", encoding="utf-8") as f:
            nb = current.read(f, "json")

        # create the module and add it to sys.modules
        # if name in sys.modules:
        #    return sys.modules[name]
        mod = types.ModuleType(fullname)
        mod.__file__ = path
        mod.__loader__ = self
        sys.modules[fullname] = mod

        # extra work to ensure that magics that would affect the user_ns
        # actually affect the notebook module's ns
        save_user_ns = self.shell.user_ns
        self.shell.user_ns = mod.__dict__

        try:
            for cell in nb.worksheets[0].cells:
                if cell.cell_type == "code" and cell.language == "python":
                    # transform the input to executable Python
                    code = self.shell.input_transformer_manager.transform_cell(cell.input)
                    # run the code in themodule
                    exec code in mod.__dict__
        finally:
            self.shell.user_ns = save_user_ns
        return mod
def test_ok():
    try:
        from IPython.display import display_html
        display_html("""<div class="alert alert-success">
        <strong>Test passed!!</strong>
        </div>""", raw=True)
    except:
        print "test ok!!"
Esempio n. 10
0
def live_map(gdx_file, variable, verbose=False):
    # Read the indicated variable(s) from the GDX file
    data = gdx_file.extract(variable)
    # Truncate unused years
    if 't' in data.coords:
        t_max = int(gdx_file.extract('t_max'))
        years = list(filter(lambda t: int(t) <= t_max, gdx_file.set('t')))
        columns = years
        data = data.sel(t=years)
    else:
        years = None
        columns = [variable]
    # Determine the coordinate containing region data
    region = 'r' if 'r' in data.coords else 'rs'
    if region == 'rs':
        data = data.sel(rs=gdx_file.set('r'))
    if 't' in data.coords:
        data = data.to_dataframe().T.stack(region)
        data.index = data.index.droplevel(0)
    else:
        data = data.to_dataframe()

    # Load map coordinates, merge, and colorize
    map_data = pd.read_hdf(join(DATA_DIR, 'province_map_data.hdf'), 'df')
    if verbose:
        print(data)
    all_data = map_data.merge(data, left_on='alpha', right_index=True)
    colored_data, data_range = color_data(all_data, columns)

    if years is not None:
        colored_data['active_year'] = t_max
        colored_data['active_value'] = colored_data[str(t_max)]
        colored_data['active_color'] = colored_data['%s_color' % t_max]

    # Plot title: description of the variable to be plotted
    TITLE = gdx_file[variable].attrs['_gdx_description']

    # Build the map
    map_box = build_map(colored_data, columns, years)

    # Output the map
    # Open our custom HTML template
    with open(join(DATA_DIR, 'map_template.jinja'), 'r') as f:
        template = Template(f.read())

    resources = Resources(mode='inline')
    # Update these to change the text
    template_variables = {
        'title': TITLE,
        'narrative': 'Data range: {}–{}'.format(data_range[0], data_range[1]),
        'tooltip_css': open(join(DATA_DIR, 'tooltip.css')).read(),
        'bokeh_min_js': resources.js_raw[0],
        }

    # Use inline resources, render the html and open
    html = file_html(map_box, resources, TITLE, template=template,
                     template_variables=template_variables)
    display_html(html, raw=True)
Esempio n. 11
0
def output_pweave():
    """
    Call this once in a Pweave document to include correct
    headers for Bokeh. Analogous to Bokeh's output_notebook
    """
    out = CDN.render_css()
    out += CDN.render_js()
    #display_markdown(out, raw=True)
    display_html(out, raw=True)
def display_file_library_ids(alias, results, valid_library):
    """Display formatted html for our report
    """
    if valid_library:
        display_html('<p>{} validated</p>'.format(alias), raw=True)
    else:
        display_html('<dl><dt>{}</dt><dd>{}</dd></dl>'.format(
            alias,
            '</br>'.join(results)), raw=True)
Esempio n. 13
0
    def _ipython_display_(self):
        if display_html is not None:
            display_html('<div id="{}" style="height: 600px; width:100%;"></div>'.format(self.__uuid), raw=True)

            display_javascript("""
                require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() {
                  document.getElementById('%s').appendChild(renderjson(%s))
                });
                """ % (self.__uuid, self.json), raw=True)
Esempio n. 14
0
def print_header(text, level=3):
    """
    Function to be used in notebooks to display headers not just plain text
    :param text: str or object to print its __repr__
    :param level: int, from 1 to 6 (1st, 2nd, 3rd order header)
    """
    from IPython.display import display_html

    display_html("<h{level}>{header}</h{level}>".format(header=text, level=level), raw=True)
Esempio n. 15
0
 def print_source(self, side, args, line):
     '''Print(if can) source of method or function or class'''
     obj = get_value_in_context(args.arg, side, self.shell)
     try:
         source = side.get_source(obj) if is_remote(obj) else get_source(obj)
     except (TypeError, IOError) as exc:
         print_err(exc, debug=debug)
         return
     display_html(highlight_python_source(source), raw=True)
def test_ok():
    """ If execution gets to this point, print out a happy message """
    try:
        from IPython.display import display_html
        display_html("""<div class="alert alert-success">
        <strong>Tests passed!!</strong>
        </div>""", raw=True)
    except:
        print "Tests passed!!"
 def grab_frame(self, **kwargs):
     img_data = io.BytesIO()
     self.fig.savefig(img_data, format='jpeg')
     img_data.seek(0)
     uri = 'data:image/jpeg;base64,' + urllib.request.quote(base64.b64encode(img_data.getbuffer()))
     img_data.close()
     clear_output(wait=True)
     display_html(HTML('<img src="' + uri + '">'))
     time.sleep(self.sleep)
Esempio n. 18
0
 def _update_output(self):
     self._output.clear_output()
     with self._output:
         df = self._df
         idxs = self.indexes
         if self.cur_frame >= 0:
             df = self._df.groupby('frame').get_group(self.cur_frame)
         #idxs = [max(idxs[0], df.index[0]), min(idxs[1], df.index[-1])]
         display_html(df[self.columns].loc[range(self.start, self.start + 50)].to_html(),
                      raw=True)
Esempio n. 19
0
 def print_mse(self, uniform_variables, efficiencies=None, stages=None, in_html=True, label=1):
     result = pandas.DataFrame(self._compute_mse(uniform_variables, efficiencies, stages=stages, label=label))
     if in_html:
         from IPython.display import display_html
         display_html("<b>Staged MSE variation</b>", raw=True)
         display_html(result)
     else:
         print("Staged MSE variation")
         print(result)
     return self
Esempio n. 20
0
def init_notebook():
    # Enable inline plotting in the notebook
    try:
        get_ipython().enable_matplotlib(gui='inline')
    except NameError:
        pass

    print('Populated the namespace with:\n' + ', '.join(__all__))
    holoviews.notebook_extension('matplotlib')
    holoviews.plotting.mpl.MPLPlot.fig_rcparams['text.usetex'] = True

    # Set plot style.
    options = Store.options(backend='matplotlib')
    options.Contours = Options('style', linewidth=2, color='k')
    options.Contours = Options('plot', aspect='square')
    options.HLine = Options('style', linestyle='--', color='b', linewidth=2)
    options.VLine = Options('style', linestyle='--', color='r', linewidth=2)
    options.Image = Options('style', cmap='RdBu_r')
    options.Image = Options('plot', title_format='{label}')
    options.Path = Options('style', linewidth=1.2, color='k')
    options.Path = Options('plot', aspect='square', title_format='{label}')
    options.Curve = Options('style', linewidth=2, color='k')
    options.Curve = Options('plot', aspect='square', title_format='{label}')
    options.Overlay = Options('plot', show_legend=False, title_format='{label}')
    options.Layout = Options('plot', title_format='{label}')
    options.Surface = Options('style', cmap='RdBu_r', rstride=1, cstride=1, lw=0.2)
    options.Surface = Options('plot', azimuth=20, elevation=8)

    # Turn off a bogus holoviews warning.
    # Temporary solution to ignore the warnings
    warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')

    module_dir = os.path.dirname(__file__)
    matplotlib.rc_file(os.path.join(module_dir, "matplotlibrc"))

    np.set_printoptions(precision=2, suppress=True,
                        formatter={'complexfloat': pretty_fmt_complex})

    # In order to make the notebooks readable through nbviewer we want to hide
    # the code by default. However the same code is executed by the students,
    # and in that case we don't want to hide the code. So we check if the code
    # is executed by one of the mooc developers. Here we do by simply checking
    # for some files that belong to the internal mooc repository, but are not
    # published.  This is a temporary solution, and should be improved in the
    # long run.

    developer = os.path.exists(os.path.join(module_dir, os.path.pardir,
                                            'scripts'))

    display_html(display.HTML(nb_html_header +
                              (hide_outside_ipython if developer else '')))

    # Patch a bug in holoviews
    from patch_holoviews import patch_all
    patch_all()
 def show_workflow_run(self, run):
     """Displays workflow run in the IPython Notebook cell
     
     Parameters
     ----------
     run: Run, Run object to be shown
     """
     run_location = Client.RUN_LOCATION % (self.__url, run.identifier) + '?embedded=true'
     iframe_code = '<iframe src="' + run_location + '" width=1200px height=900px></iframe>'
     h = HTML(iframe_code)
     display_html(h)
Esempio n. 22
0
 def _ipython_display_(self):
     json_already = json.dumps(self.already_run)
     json_to_run = json.dumps(self.to_run)
     display_html(
         '<div id="{}" style="height: 300px; width:80%;"'
         'data-already-run=\'{}\' '
         'data-to-run=\'{}\'></div>'.format(self.uuid, json_already,
                                          json_to_run),
         raw=True)
     display_javascript('window.turtle("{}");'.format(self.uuid),
                        raw=True)
Esempio n. 23
0
def display_table(rows):
    table_template = '<table>%s</table>'
    row_template = '<tr>%s</tr>'
    header_column_template = '<th>%s</th>'
    normal_column_template = '<td>%s</td>'
    rows_html = []
    for i, row in enumerate(rows):
        row_html = []
        for column in row:
            col_template = header_column_template if i == 0 else normal_column_template
            row_html.append(col_template % column)
        rows_html.append(row_template % ''.join(row_html))
    display_html(table_template % ''.join(rows_html), raw=True)
Esempio n. 24
0
 def _initialize(self):
     self.bar_id = str(uuid.uuid4())
     self.HTML = HTML(
                      """
                          <progress
                          value="{progress}"
                          max="{iterations}"
                          id="{id}">
                          </progress>
                          """.format(progress = self.progress,
                                     id = self.bar_id,
                                     iterations = self.iterations))
     display_html(self.HTML)
     self._update()
Esempio n. 25
0
def show(plot):
    """
    Include a Bokeh figure in Pweave document. Use This
    instead of ``bokeh.plotting.show``. Provides html output.

    :param plot: ``bokeh.plotting.figure`` plot to include in output.
    """

    script, div = components(plot)
    out = script
    out+= div
    #Pandoc only works if indent is removed
    #Need to display as same output, not separate, otherwise md2hml show 2 figs
    #display_markdown(dedent(out), raw=True)
    display_html(out, raw=True)
Esempio n. 26
0
    def show(self, multiples=None):
        """
        Displays the sketch on the notebook.
        """

        if multiples == 'small-multiples':
            template_name = 'multiples.html'
        elif multiples == 'select-categories':
            template_name = 'select-categories.html'
        else:
            template_name = 'base.html'
            
        rendered = self._render_(template_name)
        display_html(HTML(rendered))
        return None
Esempio n. 27
0
    def coffeescript(self, line, cell):
        opts = parse_argstring(self.coffeescript, line)
        ps = subprocess.Popen(('coffee','-sc'), stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
        ps.stdin.write(cell)
        ps.stdin.close()
        return_code = ps.wait()
        output = ps.stdout.read()
        if return_code!=0:
            print(ps.stderr.read())
            return
        if opts.verbose:
            pretty = highlight(output, self.lexer, HtmlFormatter(full=True))
            display.display_html("%s" % (pretty), raw=True)

        return display.display_javascript(output, raw=True)
Esempio n. 28
0
    def show(self, multiples=['small-multiples']):
        '''
        Forces display of the sketch on the notebook.
        '''

        if multiples == 'small-multiples':
            template_name = 'multiples.html'
        elif multiples == 'select-categories':
            template_name = 'select-categories.html'
        else:
            template_name = 'base.html'
            
        rendered = self._render_(template_name)
        display_html(HTML(rendered))
        return None
Esempio n. 29
0
def init(path=None, ipython=None):
    register_magics(ipython)

    if path is None:
        path = os.getcwd()
    persistence.connect(path)

    js_files = [
        'vis/static/d3-v3.4.11/d3.min.js',
        'vis/static/trial_graph.js',
        'vis/static/history_graph.js',
        'vis/static/ipython.js',
    ]

    require_js = '''
        <!DOCTYPE html>
        <meta charset="utf-8">
        <body>
            <script charset="utf-8">
            var now_temp = define;
            define = undefined;
            {0}
            define = now_temp;
            </script>
        </body
    '''
    js_text = [resource(js_file, 'utf-8') for js_file in js_files]

    display_html(
        require_js.format(';\n'.join(js_text)),
        raw=True
        )

    css_files = [
        'vis/static/font-awesome-4.3.0/css/font-awesome-ip.css',
        'vis/static/shared_graph.css',
        'vis/static/trial_graph.css',
        'vis/static/history_graph.css',
    ]

    css_lines = ['<style>']
    for css_file in css_files:
        css_lines.append(resource(css_file, 'utf-8'))
    css_lines.append('</style>')
    display_html('\n'.join(css_lines), raw=True)

    return "ok"
Esempio n. 30
0
    def show():
        from IPython.display import display_html

        result = []
        import matplotlib._pylab_helpers as pylab_helpers
        for manager in pylab_helpers.Gcf().get_all_fig_managers():
            result.append(ipython_inline_display(manager.canvas.figure))
        return display_html('\n'.join(result), raw=True)
Esempio n. 31
0
def hideCode():
    """Hides the code section of a jupyter notebook cell and only keep the output
    see also https://www.markroepke.me/posts/2019/06/05/tips-for-slideshows-in-jupyter.html"""

    uid = binascii.hexlify(os.urandom(8)).decode()
    html = """<div id="%s"></div>
    <script type="text/javascript">
        $(function(){
            var p = $("#%s");
            if (p.length==0) return;
            while (!p.hasClass("cell")) {
                p=p.parent();
                if (p.prop("tagName") =="body") return;
            }
            var cell = p;
            cell.find(".input").addClass("hide-in-slideshow")
        });
    </script>""" % (uid, uid)
    display_html(html, raw=True)
Esempio n. 32
0
    def _ipython_display_(self):
        f = StringIO()
        # filename = os.path.join(base_path, "cube.png")
        if self.data is not None:
            cube_png(self.data, file=f)
        else:
            self.subspace_gridded.cube_png(file=f)
        # cube64 = "'data:image/png;base64," + b64encode(file(filename).read()) + "'"
        cube64 = "'data:image/png;base64," + b64encode(f.getvalue()).decode("ascii") + "'"
        # display_javascript("""
        # window.cube_src = 'data:image/png;base64,%s';
        # """ % (cube64, colormap64), raw=True)

        self.id = id = uuid.uuid1()
        display_html("<canvas id='{id}' width=512 height=512  style='display: inline;'/>".format(**locals()), raw=True)
        display_javascript(""" $('#%s').vr(
                $.extend({cube:%s, colormap:window.colormap_src}, %s)
                )
                """ % (id, cube64, json.dumps(self.settings)), raw=True)
Esempio n. 33
0
def hide_code_in_slideshow():
    import os
    uid = os.urandom(8).encode("hex")
    html = """<div id="%s"></div>
    <script type="text/javascript">
        $(function(){
            var p = $("#%s");
            if (p.length==0) return;

            while (!p.hasClass("cell")) {
                p=p.parent();

                if (p.prop("tagName") =="body") return;
            }
            var cell = p;
            cell.find(".input").addClass("hide-in-slideshow")
        });
    </script>""" % (uid, uid)
    display_html(html, raw=True)
Esempio n. 34
0
def model_built(
    pipeline, param_grid, X, y, report=False, model_name=None, 
    test_X=None, test_y=None, **kwargs
):
    """Build the model

    Use the pipeline method and the gridsearch to build model

    Parameters:
    ------------
    pipeline: Pipeline object
        The Pipeline object
    param_grid: params dict
        It is GridSearchCV parameter
    X, y: 
        Train data
    report: boolean
        If it true, display the model, result
    model_name: string default None
    test_x, test_y:
        Test data
    """
    cv = GridSearchCV(pipeline, param_grid=param_grid, cv=3, n_jobs=2, **kwargs)

    # train model
    cv.fit(X, y=y)

    if report:
        display_html(
            '<h1>The {0} model result:</h1> \
            <ul style="font:italic 25px Fira San, Serif;"> \
                <li>The mean squared value of the test data is: {1:.4f}</li> \
                <li>The R^2 score of the test data is: {2:.4f}</li> \
                <li>The R^2 score of the train data is: {3:.4f}</li> \
            </ul>'.format(
                model_name, mean_squared_error(test_y, cv.predict(test_X)), 
                r2_score(test_y, cv.predict(test_X)),
                r2_score(y, cv.predict(X))
            ), raw=True
        )
    
    return cv
def show_app(app,  # type: dash.Dash
             port=9999,
             width=700,
             height=350,
             offline=True,
             style=True,
             **dash_flask_kwargs):
    """
    Run the application inside a Jupyter notebook and show an iframe with it
    :param app:
    :param port:
    :param width:
    :param height:
    :param offline:
    :return:
    """
    url = 'http://localhost:%d' % port
    iframe = '<iframe src="{url}" width={width} height={height}></iframe>'.format(url=url,
                                                                                  width=width,
                                                                                  height=height)
    display.display_html(iframe, raw=True)
    if offline:
        app.css.config.serve_locally = True
        app.scripts.config.serve_locally = True
    if style:
        external_css = ["https://fonts.googleapis.com/css?family=Raleway:400,300,600",
                        "https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css",
                        "http://getbootstrap.com/dist/css/bootstrap.min.css", ]

        for css in external_css:
            app.css.append_css({"external_url": css})

        external_js = ["https://code.jquery.com/jquery-3.2.1.min.js",
                       "https://cdn.rawgit.com/plotly/dash-app-stylesheets/a3401de132a6d0b652ba11548736b1d1e80aa10d/dash-goldman-sachs-report-js.js",
                       "http://getbootstrap.com/dist/js/bootstrap.min.js"]

        for js in external_js:
            app.scripts.append_script({"external_url": js})

    return app.run_server(debug=False,  # needs to be false in Jupyter
                          port=port,
                          **dash_flask_kwargs)
Esempio n. 36
0
    def _notebook_show(self, htmlid=None, display=False, annsets=None):
        from gatenlp.gatenlpconfig import gatenlpconfig
        from gatenlp.serialization.default import HtmlAnnViewerSerializer
        from IPython.display import display_html

        if not gatenlpconfig.notebook_js_initialized:
            HtmlAnnViewerSerializer.init_javscript()
            gatenlpconfig.notebook_js_initialized = True
        html = self.save_mem(
            fmt="html-ann-viewer",
            notebook=True,
            add_js=False,
            offline=True,
            htmlid=htmlid,
            annsets=annsets,
        )
        if display:
            display_html(html, raw=True)
        else:
            return html
def display_side_by_side(dfs: list, names=None, descriptions=None):
    if names is None:
        names = []
    html_str = ''
    if names is not None:
        html_str += ('<tr>' +
                     ''.join(f'<td style="text-align:center">{name}</td>'
                             for name in names) + '</tr>')

    html_str += ('<tr>' +
                 ''.join(f'<td style="vertical-align:top"> {df.to_html()}</td>'
                         for df in dfs) + '</tr>')

    if descriptions is not None:
        html_str += ('<tr>' + ''.join(f'<td>{description}</td>'
                                      for description in descriptions) +
                     '</tr>')
    html_str = f'<table>{html_str}</table>'
    html_str = html_str.replace('table', 'table style="display:inline"')
    display_html((html_str, ), raw=True)
Esempio n. 38
0
def align_figures():
    import matplotlib
    from matplotlib._pylab_helpers import Gcf
    from IPython.display import display_html
    import base64
    from ipykernel.pylab.backend_inline import show

    images = []
    for figure_manager in Gcf.get_all_fig_managers():
        fig = figure_manager.canvas.figure
        png = get_ipython().display_formatter.format(fig)[0]['image/png']
        src = base64.encodestring(png).decode()
        images.append(
            '<img style="margin:0" align="left" src="data:image/png;base64,{}"/>'
            .format(src))

    html = "{}".format("".join(images))
    show._draw_called = False
    matplotlib.pyplot.close('all')
    display_html(html, raw=True)
Esempio n. 39
0
    def _ipython_display_(self):
        try:
            column_types_json = json.dumps(self.column_types)
            data_frame_json = self.df_copy.to_json(orient='records', date_format='iso', double_precision=self.precision)

            if qgrid.remote_mode:
                cdn_base_url = "https://rawgit.com/quantopian/qgrid/master/nbextensions/qgridjs"
            else:
                cdn_base_url = "/nbextensions/qgridjs"

            raw_html = SLICK_GRID_CSS.format(div_id=self.div_id, cdn_base_url=cdn_base_url)
            raw_js = SLICK_GRID_JS.format(cdn_base_url=cdn_base_url,
                                            div_id=self.div_id,
                                            data_frame_json=data_frame_json,
                                            column_types_json=column_types_json)

            display_html(raw_html, raw=True)
            display_javascript(raw_js, raw=True)
        except Exception, err:
            display_html('ERROR: {}'.format(str(err)), raw=True)
Esempio n. 40
0
def mdisplay(dfs: List[DataFrame], names:List[str]=[]):
    """
    Displays several data frames side by side
    
    Adapded form
    https://stackoverflow.com/questions/38783027/jupyter-notebook-display-two-pandas-tables-side-by-side
    """
    
    html_str = ''
    if names:
        html_str += ('<tr>' + 
                     ''.join(f'<td style="text-align:center">{name}</td>' for name in names) + 
                     '</tr>')
    html_str += ('<tr>' + 
                 ''.join(f'<td style="vertical-align:top"> {df.to_html(index=False)}</td>' 
                         for df in dfs) + 
                 '</tr>')
    html_str = f'<table>{html_str}</table>'
    html_str = html_str.replace('table','table style="display:inline"')
    display_html(html_str, raw=True)
Esempio n. 41
0
def m4diag(li,symbols="<= v", unaryRel=""):
    # use graphviz to display a mace4 structure as a diagram
    # symbols is a list of binary symbols that define a poset or graph
    # unaryRel is a unary relation symbol that is displayed by red nodes
    i = 0
    sy = symbols.split(" ")
    #print(sy)
    st = ""
    for x in li:
        i+=1
        st+=str(i)
        uR = x.relations[unaryRel] if unaryRel!="" else [0]*x.cardinality
        for s in sy:
            t = s[:-1] if s[-1]=='d' else s
            if t in x.operations.keys():
                st+=hasse_diagram(x.operations[t],False,s[-1]=='d',uR)._repr_svg_()+"&nbsp; &nbsp; &nbsp; "
            elif t in x.relations.keys():
                st+=hasse_diagram(x.relations[t], True, s[-1]=='d',uR)._repr_svg_()+"&nbsp; &nbsp; &nbsp; "
        st+=" &nbsp; "
    display_html(st,raw=True)
def display_corr(df,
                 name,
                 corr_type,
                 top_num=20,
                 round_places=2,
                 correlation_text="r",
                 p_value_text="p",
                 sample_size_text="N",
                 text_wrap_length=50):
    #     df.index = [x[0:60] for x in df.index]
    df.index = [
        trim_strings(x) for x in df.index.str.wrap(width=text_wrap_length)
    ]

    df[correlation_text] = df[correlation_text].round(round_places)

    df1 = df.sort_values(by=correlation_text, ascending=False)[0:top_num][[
        correlation_text, p_value_text, sample_size_text
    ]]
    df2 = df.sort_values(by=correlation_text)[0:top_num][[
        correlation_text, p_value_text, sample_size_text
    ]]

    df1[p_value_text] = df1[p_value_text].apply(lambda x: "{0:0.2f}".format(x))
    df2[p_value_text] = df2[p_value_text].apply(lambda x: "{0:0.2f}".format(x))

    df1_caption = "Top " + str(
        top_num
    ) + " positive " + "(" + corr_type + ")" + " correlations for " + name
    df2_caption = "Top " + str(
        top_num
    ) + " negative " + "(" + corr_type + ")" + " correlations for " + name

    df1_styler = df1.style.set_table_attributes(
        "style='display:inline'").set_caption(df1_caption)
    df2_styler = df2.style.set_table_attributes(
        "style='display:inline'").set_caption(df2_caption)

    display_html(df1_styler._repr_html_().replace("\\n", "<br />") +
                 df2_styler._repr_html_().replace("\\n", "<br />"),
                 raw=True)
Esempio n. 43
0
    def __init__(self):
        self.comm = None

        folder = os.path.dirname(__file__)
        with open(os.path.join(folder, "../js", "initIPython.js"), "r") as fd:
            initIPython = fd.read()

        display_javascript(Javascript(initIPython))

        css = """
            <style>
                div.output_area img, div.output_area svg {
                    max-width: 100%;
                    height: 100%;
                }
            </style>
        """
        display_html(HTML(css))

        time.sleep(0.5)
        self.comm = Comm(target_name='nvd3_stat', data={'event': 'open'})
Esempio n. 44
0
    def export(self, value=None):
        self.update_table()
        self.remote_js = True
        div_id = str(uuid.uuid4())
        grid_options = json.loads(self.grid_options)
        grid_options['editable'] = False

        raw_html = SLICK_GRID_CSS.format(
            div_id=div_id,
            cdn_base_url=self._cdn_base_url,
        )
        raw_js = SLICK_GRID_JS.format(
            cdn_base_url=self._cdn_base_url,
            div_id=div_id,
            data_frame_json=self._df_json,
            column_types_json=self._column_types_json,
            options_json=json.dumps(grid_options),
        )

        display_html(raw_html, raw=True)
        display_javascript(raw_js, raw=True)
Esempio n. 45
0
def DisplayDMRBTables(DMRB_ST, DMRB_LT, S, L):
    DMRB_ST.fillna(0, inplace=True)
    DMRB_LT.fillna(0, inplace=True)
    Table1 = DMRB_ST.set_index('Change').rename_axis(None)
    Table2 = DMRB_LT.set_index('Change').rename_axis(None)
    df1_styler = Table1.style.set_table_attributes("style='display:inline'") \
                .set_caption('Short-term DMRB LA111 (Greatest change)') \
                .format({'ST Day RES': '{:,.0f}' \
                .format,'ST Day OSR': '{:,.0f}' \
                .format,'ST Night RES': '{:,.0f}' \
                .format,'ST Night OSR': '{:,.0f}'})
    df2_styler = Table2.style.set_table_attributes("style='display:inline'") \
                .set_caption('Long-term DMRB LA111 (Greatest change)') \
                .format({'LT Day RES': '{:,.0f}' \
                .format,'LT Day OSR': '{:,.0f}' \
                .format,'LT Night RES': '{:,.0f}' \
                .format,'LT Night OSR': '{:,.0f}'})
    if S == 1 and L == 1:
        display_html(df1_styler._repr_html_() + " " + df2_styler._repr_html_(),
                     raw=True)
    if S == 1 and L == 0:
        display_html(df1_styler._repr_html_(), raw=True)
    if S == 0 and L == 1:
        display_html(df2_styler._repr_html_(), raw=True)
    return ()
Esempio n. 46
0
        def show_next():
            nonlocal current_index
            current_index += 1
            set_label_text()

            if current_index >= len(sample):

                for btn in buttons:
                    btn.disabled = True

                with out:
                    clear_output(wait=True)
                    print("\033[1mThat's all folks!\033[0m\n")

            else:
                with out:
                    clear_output(wait=True)
                    print("\033[1mText:\033[0m")

                    doc = self.nlp(sample[col_text][current_index])
                    if self.model is None:
                        doc.ents = []

                    else:
                        doc.ents = [
                            ent for ent in doc.ents
                            if ent.label_ in self.labels
                        ]

                    for label in self.labels:
                        textboxes[label].value = self.delimiter.join(
                            list(
                                set(ent.text for ent in doc.ents
                                    if ent.label_ == label)))

                    ## NOTE displacy complains if there are no ents
                    with warnings.catch_warnings():
                        warnings.filterwarnings("ignore")
                        html = displacy.render(doc, style="ent")
                        display_html(HTML(html), raw=False)
Esempio n. 47
0
    def ____ipython_display_(self):
        #base64 = file(os.path.join(base_path, "data.png")).read().encode("base64").replace("\n", "")
        #base64_colormap  = file(os.path.join(base_path, "colormap.png")).read().encode("base64").replace("\n", "")
        #print base64[:10]
        #code = "base64 = '" + base64 + "'; base64_colormap = '" + base64_colormap + "';"
        display_javascript(open(os.path.join(base_path, "all.js")).read(),
                           raw=True)
        display_javascript(file(
            os.path.join(base_path, "vaex_volumerendering.js")).read(),
                           raw=True)
        display_html(file(os.path.join(base_path, "snippet.js")).read(),
                     raw=True)
        html1 = file(os.path.join(base_path, "snippet.html")).read()
        display_html(html1, raw=True)
        #print "ok"
        display_html("""<div>BLAAT</div> """, raw=True)

        if 0:
            js1 = file(os.path.join(base_path, "snippet.js")).read()
            js2 = file(os.path.join(base_path,
                                    "vaex_volumerendering.js")).read()
            js_lib = file(os.path.join(base_path, "all.js")).read()
            html1 = file(os.path.join(base_path, "snippet.html")).read()
            HTML("<script>" + js_lib + "\n" + code + "</script>" + "<script>" +
                 js2 + "</script>" + html1 + js1)
Esempio n. 48
0
def wavejson_to_wavedrom(wavejson, width=None, skin='default'):
    '''
    Create WaveDrom display from WaveJSON data.

    This code is from https://github.com/witchard/ipython-wavedrom.

    Inputs:
      width: Width of the display window in pixels. If left as None, the entire
             waveform will be squashed into the width of the page. To prevent
             this, set width to a large value. The display will then become scrollable.
      skin:  Selects the set of graphic elements used to draw the waveforms.
             Allowable values are 'default' and 'narrow'.
    '''

    # Set the width of the waveform display.
    style = ''
    if width != None:
        style = ' style="width: {w}px"'.format(w=str(int(width)))

    # Generate the HTML from the JSON.
    htmldata = '<div{style}><script type="WaveDrom">{json}</script></div>'.format(
        style=style, json=json.dumps(wavejson))
    DISP.display_html(DISP.HTML(htmldata))

    # Trigger the WaveDrom Javascript that creates the graphical display.
    DISP.display_javascript(
        DISP.Javascript(
            data='WaveDrom.ProcessAll();',
            lib=[
                'http://wavedrom.com/wavedrom.min.js',
                'http://wavedrom.com/skins/{skin}.js'.format(skin=skin)
            ]))

    # The following allows the display of WaveDROM in the HTML files generated by nbconvert.
    # It's disabled because it makes Github's nbconvert freak out.
    setup = '''
<script src="http://wavedrom.com/skins/{skin}.js" type="text/javascript"></script>
<script src="http://wavedrom.com/wavedrom.min.js" type="text/javascript"></script>
<body onload="WaveDrom.ProcessAll()">
    '''.format(skin=skin)
Esempio n. 49
0
def display_together_with_labels(dfs, captions=[], spacing=36, *args):
    "incomplete: don't use this function"
    
    
    "Displays pandas dataframes together in Jupyter Notebook"
#     dfs: list of dataframes to display
#     captions: list of captions for each dataframe
#     spacing: spaces between dataframes when displayed
    
    # If captions are none, must create an array of empty captions
    if len(captions) < len(dfs):
        missing = len(dfs) - len(captions)
        captions += ['']*missing
    
    html_str = ''
    
    # Obtain HTML for each dataframe
    for i in range(len(dfs)):
        
        df_styler = dfs[i].reset_index(drop=True).style.\
                set_table_attributes("style='display:inline'").\
                set_caption(captions[i])
        
#         df_styler = df_styler.hide_index()
        html_str += df_styler._repr_html_()
        html_str += '&nbsp;' * spacing
    html_str += '<br>'*2
    
    # Hide df labels with BeautifulSoup
    soup = BeautifulSoup(html_str, 'html.parser')

    tags = soup.find_all('col1')
    for tag in tags:
        tag['hidden']=False
        tag['style']='bold'
        
    html_str = str(soup)
    
    # Display HTML
    display_html(html_str, raw=True)
Esempio n. 50
0
 def _update_output(self, out):
     out.clear_output()
     idx = {}
     df = pd.DataFrame([])
     for sdx, scn in enumerate(self.active()):
         if not scn.selected:
             continue
         elif len(scn.selected['idx']) == 0:
             continue
         idx[sdx] = [
             int(''.join(filter(lambda x: x.isdigit(), i)))
             for i in scn.selected['idx']
         ]
         if len(idx[sdx]) % 2 != 0:
             raise ValueError(
                 "Must select an even number of atoms. Last selected atom has been truncated."
             )
         atom_coords = self._uniatom[sdx].groupby('frame').get_group(scn.frame_idx). \
                            reset_index(drop=True).loc[[i for i in idx[sdx]], ['x', 'y', 'z']]
         atom_coords.set_index([[i for i in range(len(atom_coords))]],
                               inplace=True)
         distance = [
             self._get_distance(
                 atom_coords.loc[i, ['x', 'y', 'z']].values,
                 atom_coords.loc[i + 1, ['x', 'y', 'z']].values)
             for i in range(0, len(atom_coords), 2)
         ]
         distance = [i * Length['au', 'Angstrom'] for i in distance]
         with out:
             df = pd.concat([
                 df,
                 pd.DataFrame([[
                     distance[int(i / 2)], idx[sdx][i], idx[sdx][i + 1], sdx
                 ] for i in range(0, len(idx[sdx]), 2)],
                              columns=[
                                  "dr (Angs.)", "adx0", "adx1", "scene"
                              ])
             ])
     with out:
         display_html(df.to_html(), raw=True)
Esempio n. 51
0
def show_ipyvtk(plotter, return_viewer):
    """Display an interactive viewer widget using ``ipyvtk_simple``."""
    if any('SPYDER' in name for name in os.environ):
        warnings.warn('``use_ipyvtk`` is incompatible with Spyder.\n'
                      'Use notebook=False for interactive '
                      'plotting within spyder or disable it globally with:\n'
                      'pyvista.set_jupyter_backend(None)')

    try:
        from ipyvtk_simple.viewer import ViewInteractiveWidget
    except ImportError:  # pragma: no cover
        raise ImportError('Please install `ipyvtk_simple` to use this feature:'
                          '\thttps://github.com/Kitware/ipyvtk-simple')

    # Have to leave the Plotter open for the widget to use
    disp = ViewInteractiveWidget(
        plotter.ren_win,
        on_close=plotter.close,
        transparent_background=plotter.image_transparent_background)

    if return_viewer:
        return disp
    display.display_html(disp)
def Audio(data, rate, filename=None, display_audio=True):
    """Save data to a file then play."""
    # Get a filename
    if filename is None:
        (_, filename) = tempfile.mkstemp(".wav", "comp28512_")

    # If none of the values are outside the range +/- 1 we remind the student
    # to scale!
    if data.dtype.kind == 'f':
        if np.max(data) <= 1.0 and np.min(data) >= -1.0:
            logger.warn("Data values fall in the range +/- 1.0, you need to "
                        "scale by 32767 (2^15 - 1) to listen to audio.")
        else:
            logger.warn("Casting data to int16.")
            data = np.int16(data)

    # Write the data
    wavfile.write(filename, rate, data)
    print("Data written to {}.".format(filename))

    # Read the data back in, display as HTML
    if display_audio:
        display.display_html(audio_from_file(filename))
Esempio n. 53
0
    def print_explanation(self):
        for i in range(len(self.explanation)):
            run = i + 1
            print("Run {}: Column Shifts / Classification Report".format(run))
            explanation = self.explanation[i]['data']
            information = self.information[i]

            if is_in_jupyter():
                relative_accuracy = self.explanation[i]['relative_accuracy']
                explanation = explanation.style.apply(lambda x: [
                    'background: #FF6A6A'
                    if x['accuracy in %'] < relative_accuracy else ''
                ] * len(x),
                                                      axis=1)
                html_str = ''
                html_str += explanation.render()
                html_str += information.to_html()
                display_html(html_str.replace('table',
                                              'table style="display:inline"'),
                             raw=True)
            else:
                print(explanation, '\n')
                print(information, '\n')
Esempio n. 54
0
def preview_message(message_html):
    """Previews an HTML string.
    
    If we are running in IPython, use it display machinary 
    to show a preview of the HTML message. Otherwise, show
    the message in a web browser.
    """

    if run_from_ipython():
        from IPython.display import display_html

        display_html(message_html, raw=True)
        display_html("<hr>", raw=True)
    else:
        import tempfile
        from pathlib import Path
        import webbrowser

        with tempfile.NamedTemporaryFile(delete=False,
                                         suffix=".html") as out_file:
            file_path = Path(out_file.name)
            out_file.write(message_html.encode("utf8"))
            webbrowser.open(file_path.as_uri())
Esempio n. 55
0
def mostrar_dfs(
    *dfs: Union[pd.DataFrame, pd.Series], espacio: int = 20
) -> None:
    """Muestra más de un DataFrame o Serie de pandas en una celda de jupyter notebooks.

    Parameters
    ----------
    dfs : Union[pd.DataFrame, pd.Series]
        Los objetos (DataFrame o Series de pandas) que quieres mostrar.
    espacio : int, optional
        El espacio entre cada DataFrame en pixeles, por default 20 
    """
    html = f"""<div style="display:flex;">"""
    for df in dfs:
        try:
            html_del_df = df._repr_html_()
        except AttributeError:
            html_del_df = f"<pre>{repr(df)}</pre>"

        html += f"<div style='margin-right:{espacio}px;'>{html_del_df}</div>"

    html += "</div>"
    display_html(html, raw=True)
Esempio n. 56
0
    def start(self):
        super(ProgressBarIPyNb, self).start()
        if not self.quiet:
            display_javascript(
                '$("[data-key=\'%(key)s\']").parent().parent().remove()' %
                self,
                raw=True)
            display_html('''
            <style>
                .progress {
                    position: relative;
                }

                .progress > .text {
                    position: absolute;
                    left: 0;
                    width: 100%%;
                    text-align: center;
                }

                .progress .bar {
                    transition-property: none;
                }
            </style>

            <h3>%(title)s:</h3>
            <div class="progress" id="%(html_id)s" data-key=%(key)s>
                <div class="bar bar-success completed-part" style="width: 0%%;"></div>
                <div class="bar bar-warning running-part" style="width: 100%%;"></div>
                <div class="text">
                    <span class="main">Starting...</span>
                    <span class="extra"></span>
                </div>
            </div>
            ''' % self,
                         raw=True)
        self.output_change_value()
Esempio n. 57
0
    def compare_artifact_pair_and_execution_properties(self, artifact_id,
                                                       other_artifact_id,
                                                       execution_type_name):
        """Displays properties of 2 artifacts and executions that generated them.

    Args:
      artifact_id: A `int` indicating the id of one artifact.
      other_artifact_id: A `int` indicating the id of another artifact.
      execution_type_name: A `str` indicating the type of executions that
          generated `artifact_id` and `other_artifact_id`.
    """
        # Get data frame to visualize properties of the 2 artifacts.
        df = self.get_df_from_artifacts_or_executions(
            self.metadata_store.get_artifacts_by_id(
                [artifact_id, other_artifact_id]))
        artifacts_df_styler = df.style.set_caption(
            'Properties for Artifacts {}, {}'.format(artifact_id,
                                                     other_artifact_id))

        # Compare properties of the executions that generated these artifacts.
        execution = self.get_execution_for_output_artifact(
            artifact_id, execution_type_name)
        other_execution = self.get_execution_for_output_artifact(
            other_artifact_id, execution_type_name)
        if not execution or not other_execution:
            return
        executions_df = self.get_df_from_artifacts_or_executions(
            [execution, other_execution])
        executions_df_styler = executions_df.style.set_caption(
            'Properties for Executions that generated Artifacts {}, {}'.format(
                artifact_id, other_artifact_id))

        # Display the HTML.
        # pylint: disable=protected-access
        display_html(artifacts_df_styler._repr_html_() +
                     executions_df_styler._repr_html_(),
                     raw=True)
Esempio n. 58
0
def display_synth_data(directory=None, rows=None):
    container = ''

    if directory is None:
        directory = '/content/CIS-700/results/'
    if rows is None:
        rows = 5
    else:
        rows = int(rows)

    real_synth_image_path = directory + "real_synth_data.png"

    for filename in os.listdir(directory):
        if filename.startswith(test_file_pref) and filename.endswith(txt_ext):
            fn_split = filename.split(test_file_pref)[1].split(
                txt_ext)[0].split('_')
            if len(fn_split) == 2:
                model = fn_split[0]
                training = fn_split[1]
                df = pd.read_csv(directory + filename, sep="\n", header=None)
                df.columns = [
                    model.capitalize() + " " + training.capitalize() +
                    " Synth Data"
                ]
                df_styler = df.head(rows).style.set_table_attributes(
                    "style='display:inline-block'")
                if container != '':
                    container += '<hr style="width: 400px; margin-left:0;">'
                container += df_styler._repr_html_()

    if container != '':

        file = open(directory + "real_synth_data.html", "w")
        file.write(container)
        file.close()
        display_html(container, raw=True)
        '''
Esempio n. 59
0
def display_grids(df1,
                  df2,
                  caption1="First simulation",
                  caption2="Second simulation"):
    # create output widgets
    widget1 = widgets.Output()
    widget2 = widgets.Output()
    # render in output widgets
    with widget1:
        display(df1.style.set_caption(caption1))
        #df1.info()
    with widget2:
        display(df2.style.set_caption(caption2))
        #df1.info()
    # add some CSS styles to distribute free space
    box_layout = Layout(display='flex',
                        flex_flow='row',
                        justify_content='space-around',
                        width='auto')
    # create Horizontal Box container
    hbox = widgets.HBox([widget1, widget2], layout=box_layout)
    # render hbox
    display_html(hbox)
    return
Esempio n. 60
0
def render_html(results):
    '''
    Render an ODS-like HTML report

    Parameters
    ----------
    results : CASResults object

    Returns
    -------
    None

    '''
    if hasattr(results, '_render_html_'):
        out = results._render_html_()
        if out is not None:
            return display_html(HTML(STYLESHEET + out))

    if hasattr(results, '_repr_html_'):
        out = results._repr_html_()
        if out is not None:
            return display_html(HTML(out))

    return display_html(HTML('<pre>%s</pre>' % pformat(results)))