Пример #1
0
def __pre_run_cell__():
    # type: () -> None
    """ Like pre_run_cell, but is triggered prior to any execution.

    Sometimes code can be executed by libraries, etc. which skipping the
    history/display mechanisms, in which cases pre_run_cell will not fire.

    :return: None
    """
    global POST_MESSAGE
    messages = STDW.get_messages()
    found_errors = False
    runtime_crashed = False
    if messages:
        for message in messages:
            if message.startswith("[ERRMGR]"):
                found_errors = True
                # Errors found, but maybe not critical, like for example
                # tasks that failed but recovered.
            if message == "[ERRMGR]  -  Shutting down COMPSs...":
                # A critical error occurred --> notify that COMPSs runtime
                # stopped working to avoid issues when running any PyCOMPSs
                # function.
                runtime_crashed = True
        if runtime_crashed:
            # Display popup with the error messages
            current_flags = str(os.environ["PYCOMPSS_CURRENT_FLAGS"])
            header = []  # type: typing.List[str]
            footer = []  # type: typing.List[str]
            popup_body = header + messages + footer
            error_messages_html = "<p>" + "<br>".join(popup_body) + "</p>"
            error_messages_html = error_messages_html.replace("'", "")
            popup_title_html = "COMPSs RUNTIME STOPPED"
            popup_code = """require(["base/js/dialog"],
                                    function(dialog) OPENBRACKET
                                        function restartCOMPSs()OPENBRACKET
                                            var kernel = IPython.notebook.kernel;
                                            kernel.execute("import base64; import json; from pycompss.interactive import stop, start; stop(_hard_stop=True); _COMPSS_START_FLAGS=json.loads(base64.b64decode('" + '{2}' + "'.encode())); start(**_COMPSS_START_FLAGS)");
                                        CLOSEBRACKET
                                        function continueWithoutCOMPSs()OPENBRACKET
                                            var kernel = IPython.notebook.kernel;
                                            kernel.execute("from pycompss.interactive import stop; stop(_hard_stop=True)");
                                        CLOSEBRACKET
                                        dialog.modal(OPENBRACKET
                                            title: '{0}',
                                            body: $('{1}'),
                                            buttons: OPENBRACKET
                                                'Continue without COMPSs': OPENBRACKET
                                                                             click: function() OPENBRACKET
                                                                                 continueWithoutCOMPSs();
                                                                             CLOSEBRACKET
                                                                           CLOSEBRACKET,
                                                'Restart COMPSs': OPENBRACKET
                                                                    class: 'btn-primary',
                                                                    click: function() OPENBRACKET
                                                                        restartCOMPSs();
                                                                    CLOSEBRACKET
                                                                  CLOSEBRACKET
                                            CLOSEBRACKET
                                        CLOSEBRACKET);
                                    CLOSEBRACKET
                            );""".format(popup_title_html, error_messages_html,
                                         current_flags)  # noqa: E501
            popup_js = popup_code.replace("OPENBRACKET",
                                          '{').replace("CLOSEBRACKET",
                                                       '}')  # noqa: E501
            popup = Javascript(popup_js)
            display(popup)  # noqa
            warn_msg = "WARNING: Some objects may have not been synchronized and need to be recomputed."  # noqa: E501
            POST_MESSAGE = "\x1b[40;43m" + warn_msg + "\x1b[0m"
        elif found_errors:
            # Display popup with the warning messages
            header = []
            footer = []
            popup_body = header + messages + footer
            error_messages_html = "<p>" + "<br>".join(popup_body) + "</p>"
            error_messages_html = error_messages_html.replace("'", "")
            popup_title_html = "WARNING: Some tasks may have failed"
            popup_code = """require(["base/js/dialog"],
                                    function(dialog) OPENBRACKET
                                        dialog.modal(OPENBRACKET
                                            title: '{0}',
                                            body: $('{1}'),
                                            buttons: OPENBRACKET
                                                'Continue': OPENBRACKET CLOSEBRACKET,
                                            CLOSEBRACKET
                                        CLOSEBRACKET);
                                    CLOSEBRACKET
                            );""".format(popup_title_html, error_messages_html)
            popup_js = popup_code.replace("OPENBRACKET",
                                          '{').replace("CLOSEBRACKET",
                                                       '}')  # noqa: E501
            popup = Javascript(popup_js)
            display(popup)  # noqa
            info_msg = "INFO: The runtime has recovered the failed tasks."
            POST_MESSAGE = "\x1b[40;46m" + info_msg + "\x1b[0m"
        else:
            # No issue
            pass
Пример #2
0
 def display_js(self):
     # XXX How to do this just once? It has to deal with multiple
     # browser instances using the same kernel (require.js - but the
     # file isn't static?).
     display(Javascript(FigureManagerNbAgg.get_javascript()))
Пример #3
0
        for f in nb:
            if f == 'vpython_data':
                if len(os.listdir(nbdata)) == datacnt:
                    dataready = True
            if f == 'vpython_libraries':
                if len(os.listdir(nblib)) == libcnt:
                    libready = True
        if libready and dataready: break
    # Mark with the version number that the files have been transferred successfully:
    fd = open(nbdir + '/vpython_version.txt', 'w')
    fd.write(__version__)
    fd.close()

display(
    Javascript(
        """if (typeof Jupyter !== "undefined") {require.undef("nbextensions/vpython_libraries/glow.min");}else{element.textContent = ' ';}"""
    ))
display(
    Javascript(
        """if (typeof Jupyter !== "undefined") {require.undef("nbextensions/vpython_libraries/glowcomm");}else{element.textContent = ' ';}"""
    ))
display(
    Javascript(
        """if (typeof Jupyter !== "undefined") {require.undef("nbextensions/vpython_libraries/jquery-ui.custom.min");}else{element.textContent = ' ';}"""
    ))

display(
    Javascript(
        """if (typeof Jupyter !== "undefined") {require(["nbextensions/vpython_libraries/glow.min"], function(){console.log("GLOW LOADED");});}else{element.textContent = ' ';}"""
    ))
display(
Пример #4
0
 def display(self):
     display(Javascript("createTrees([" + self.to_json() + "])"))
     display(Javascript("updateTrees()"))
Пример #5
0
def run(command):
    display(Javascript(command))
Пример #6
0
    def show_data_widget(self, ref, title="", cell_id=None, tag="release"):
        """
        Renders a widget using the generic kbaseNarrativeOutputWidget container.

        Parameters
        ----------
        ref : string
            Reference to object turning into parameters that get fed into the widget.
            This reference may include ref-path.
        """

        widget_name = "kbaseNarrativeDataCell"
        input_data = dict()
        info_tuple = clients.get('workspace').get_object_info_new({
            'objects': [{
                'ref': ref
            }],
            'includeMetadata':
            1
        })[0]
        input_data['info_tuple'] = info_tuple

        bare_type = info_tuple[2].split('-')[0]

        type_spec = self._sm.get_type_spec(bare_type, raise_exception=False)
        if type_spec is None:
            input_data[
                'error_message'] = "Type-spec wasn't found for '" + bare_type + "'"
        elif 'view_method_ids' not in type_spec or len(
                type_spec['view_method_ids']) != 1:
            input_data['error_message'] = (
                "Type-spec for '" + bare_type + "' should " +
                "have exactly one ID in 'view_method_ids' field")
        else:
            input_data['type_spec'] = type_spec
            method_id = type_spec['view_method_ids'][0]
            spec = self._sm.get_spec(method_id, tag=tag)
            input_data['app_spec'] = spec

            # Let's build output according to mappings in method-spec
            spec_params = self._sm.app_params(spec)
            input_params = {}
            is_ref_path = ';' in ref
            is_external = info_tuple[7] != os.environ['KB_WORKSPACE_ID']
            # it's not safe to use reference yet (until we switch to them all over the Apps)
            # But in case we deal with ref-path we have to do it anyway:
            obj_param_value = ref if (is_ref_path
                                      or is_external) else info_tuple[1]
            for param in spec_params:
                if any(t == bare_type for t in param['allowed_types']):
                    input_params[param['id']] = obj_param_value

            (input_params,
             ws_refs) = validate_parameters(method_id, tag, spec_params,
                                            input_params)
            (output_widget, output) = map_outputs_from_state([], input_params,
                                                             spec)
            input_data['output'] = output

        if cell_id is not None:
            cell_id = "\"{}\"".format(cell_id)

        input_template = """
        element.html("<div id='{{input_id}}' class='kb-vis-area'></div>");

        require(['kbaseNarrativeOutputCell'], function(KBaseNarrativeOutputCell) {
            var w = new KBaseNarrativeOutputCell($('#{{input_id}}'), {
                "data": {{input_data}},
                "type":"viewer",
                "widget":"{{widget_name}}",
                "cellId":{{cell_id}},
                "title":"{{cell_title}}",
                "time":{{timestamp}}
            });
        });
        """

        js = Template(input_template).render(
            input_id=self._cell_id_prefix + str(uuid.uuid4()),
            widget_name=widget_name,
            input_data=json.dumps(input_data),
            cell_title=title,
            cell_id=cell_id,
            timestamp=int(round(time.time() * 1000)))
        return Javascript(data=js, lib=None, css=None)
Пример #7
0
def window_open(url):
    display(Javascript('window.open("{url}");'.format(url=url)))
Пример #8
0
# TODO: protected imports?
from IPython.html import widgets
from IPython.utils.traitlets import Unicode
from IPython.display import Javascript, display

import plotly.plotly.plotly as py
from plotly import utils, tools
from plotly.graph_objs import Figure

# Load JS widget code
# No officially recommended way to do this in any other way
# http://mail.scipy.org/pipermail/ipython-dev/2014-April/013835.html
js_widget_code = resource_string('plotly',
                                 'widgets/graphWidget.js').decode('utf-8')

display(Javascript(js_widget_code))

__all__ = None


class GraphWidget(widgets.DOMWidget):
    """An interactive Plotly graph widget for use in IPython
    Notebooks.
    """
    _view_name = Unicode('GraphView', sync=True)
    _message = Unicode(sync=True)
    _graph_url = Unicode(sync=True)
    _new_url = Unicode(sync=True)
    _filename = ''
    _flags = {'save_pending': False}
Пример #9
0
        def __init__(self, env, error_traceback=False):
            self.id = uuid.uuid1().hex
            self.error_traceback = error_traceback
            env.error_handler = handle_error_nostop
            self.env = env

            register_callback('reset_' + self.id, self.reset)

            register_callback('step_' + self.id, self.step)

            board_height, board_width = self.env.observation_space.shape

            display(
                HTML("""
            <style>
            .ttt_container .row {
                display:inline-block;
            }
            .ttt_container .cell-wrapper {
                margin: 0.2em;
                border: solid 1px;
                text-align: center;
                width: 5em;
                height: 5em;
                vertical-align: middle;
                display: table;
            }

            .ttt_container .cell {
                vertical-align: middle;
                display: table-cell;
                font-size: 200%;
            }

            .ttt_container, .ttt_container .ttt_grid {
                display: inline-block;
            }

            .ttt_container button {
                display: block;
                width: 100%;
                margin-top: 0.5em;
                height: 2.5em;
            }

            .ttt_winning {
                color: green;
            }

            .ttt_losing {
                color: red;
            }

            .ttt_done {
                opacity: 0.5;
            }
            </style>

            <div id="ttt_container_{{UUID_STR}}"></div>
            """.replace("{{UUID_STR}}", self.id)))

            display(
                Javascript(jupyter_javascript_routines +
                           ttc_javascript(self.id, board_height, board_width)))
Пример #10
0
 def _addJavascript(self, javascript):
     ipythonDisplay(Javascript(javascript))
Пример #11
0
 def _addScriptElements(self):
     if len(self.scripts) == 0:
         return
     ipythonDisplay(Javascript(self.renderTemplate('addScriptElements.js')))
Пример #12
0
    def __init__(self,
                 images,
                 name="iterator",
                 show_name=True,
                 show_axis=False,
                 show_random=True,
                 fig_size=(10, 10),
                 buttons_vertical=False,
                 image_display_function=None):
        if len(images) == 0:
            raise Exception("No images provided")

        self.show_axis = show_axis
        self.name = name
        self.show_name = show_name
        self.show_random = show_random
        self.images = images
        self.max_pos = len(self.images) - 1
        self.pos = 0
        self.fig_size = fig_size
        self.buttons_vertical = buttons_vertical

        if image_display_function is None:
            self.image_display_function = self.__show_image
        else:
            self.image_display_function = image_display_function

        self.previous_button = self.__create_button("Previous",
                                                    (self.pos == 0),
                                                    self.__on_previous_clicked)
        self.next_button = self.__create_button("Next",
                                                (self.pos == self.max_pos),
                                                self.__on_next_clicked)
        self.save_button = self.__create_button("Save", False,
                                                self.__on_save_clicked)
        self.save_function = self.__save_function  # save_function

        buttons = [self.previous_button, self.next_button]

        if self.show_random:
            self.random_button = self.__create_button("Random", False,
                                                      self.__on_random_clicked)
            buttons.append(self.random_button)

        buttons.append(self.save_button)

        label_total = Label(value='/ {}'.format(len(self.images)))
        self.text_index = BoundedIntText(value=1, min=1, max=len(self.images))
        self.text_index.layout.width = '80px'
        self.text_index.layout.height = '35px'
        self.text_index.observe(self.__selected_index)
        self.out = Output()
        self.out.add_class(name)

        if self.buttons_vertical:
            self.all_widgets = HBox(children=[
                VBox(children=[HBox([self.text_index, label_total])] +
                     buttons), self.out
            ])
        else:
            self.all_widgets = VBox(children=[
                HBox([self.text_index, label_total]),
                HBox(children=buttons), self.out
            ])
        ## loading js library to perform html screenshots
        j_code = """
                require.config({
                    paths: {
                        html2canvas: "https://html2canvas.hertzen.com/dist/html2canvas.min"
                    }
                });
            """
        display(Javascript(j_code))
Пример #13
0
 def setVisible(self, obj, tf):
     display(Javascript('%s.setVisible(\"%s\", %s)' % (self.instanceName, obj, str(tf).lower())))
     return
Пример #14
0
def save_notebook():
    Javascript("IPython.notebook.save_notebook()")
Пример #15
0
def notebook_save():
    display(Javascript('IPython.notebook.save_checkpoint();'))
Пример #16
0
def exec_remaining_cells():
    """Execute all cells below currently selected cell."""
    Javascript("Jupyter.notebook.execute_cells_below()")
Пример #17
0
    def show_advanced_viewer_widget(self,
                                    widget_name,
                                    params,
                                    output_state,
                                    tag="release",
                                    title="",
                                    type="method",
                                    cell_id=None,
                                    check_widget=False,
                                    **kwargs):
        """
        Renders a widget using the generic kbaseNarrativeOutputWidget container.

        Parameters
        ----------
        widget_name : string
            The name of the widget to print the widgets for.
        params : dict
            The dictionary of parameters that gets fed into the widget.
        tag : string, default="release"
            The version tag to use when looking up widget information.
        type : string, default="method"
            The type of output widget to show (options = method,app,viewer)
        check_widget: boolean, default=True
            If True, checks for the presense of the widget_name and get its known constants from
            the various app specs that invoke it. Raises a ValueError if the widget isn't found.
            If False, skip that step.
        **kwargs:
            These vary, based on the widget. Look up required variable names
            with WidgetManager.print_widget_inputs()
        """

        input_data = dict()

        if check_widget:
            check_tag(tag, raise_exception=True)
            if widget_name not in self.widget_info[tag]:
                raise ValueError("Widget %s not found with %s tag!" %
                                 (widget_name, tag))
            input_data = self.get_widget_constants(widget_name, tag)

        # Let the kwargs override constants
        input_data.update(params)

        if cell_id is not None:
            cell_id = "\"{}\"".format(cell_id)

        input_template = """
        element.html("<div id='{{input_id}}' class='kb-vis-area'></div>");
        require(['widgets/custom/advancedViewerOutputWrapper'], function(wrapperWidget) {
            wrapperWidget.launchWidget({
                id: '{{input_id}}',
                data: {{input_data}},
                state: {{output_state}},
                widget: '{{widget_name}}',
                cellId: {{cell_id}},
                title: '{{cell_title}}',
                time: {{timestamp}}
            });
        });
        """

        js = Template(input_template).render(
            input_id=self._cell_id_prefix + str(uuid.uuid4()),
            widget_name=widget_name,
            input_data=json.dumps(input_data),
            output_state=json.dumps(output_state),
            cell_title=title,
            cell_id=cell_id,
            timestamp=int(round(time.time() * 1000)))
        return Javascript(data=js, lib=None, css=None)
Пример #18
0
    def run(self,
            mapper,
            aggregator=None,
            reducer=None,
            number_of_trajectories=None,
            chunk_size=None,
            verbose=True,
            progress_bar=True,
            store_realizations=True,
            storage_mode="Shared",
            cache_results=False):
        """ Main entry point """
        if store_realizations:
            if self.storage_mode is None:
                if storage_mode != "Persistent" and storage_mode != "Shared":
                    raise MolnsUtilException(
                        "Acceptable values for 'storage_mode' are 'Persistent' or 'Shared'"
                    )
                self.storage_mode = storage_mode
            elif self.storage_mode != storage_mode:
                raise MolnsUtilException(
                    "Storage mode already set to {0}, can not mix storage modes"
                    .format(self.storage_mode))
            # Do we have enough trajectores yet?
            if number_of_trajectories is None and self.number_of_trajectories == 0:
                raise MolnsUtilException("number_of_trajectories is zero")
            # Run simulations
            if self.number_of_trajectories < number_of_trajectories:
                self.add_realizations(number_of_trajectories -
                                      self.number_of_trajectories,
                                      chunk_size=chunk_size,
                                      verbose=verbose,
                                      storage_mode=storage_mode)

            if chunk_size is None:
                chunk_size = self._determine_chunk_size(
                    self.number_of_trajectories)
            if verbose:
                print "Running mapper & aggregator on the result objects (number of results={0}, chunk size={1})".format(
                    self.number_of_trajectories * len(self.parameters),
                    chunk_size)
            else:
                progress_bar = False

            # chunks per parameter
            num_chunks = int(
                math.ceil(self.number_of_trajectories / float(chunk_size)))
            chunks = [chunk_size] * (num_chunks - 1)
            chunks.append(self.number_of_trajectories - chunk_size *
                          (num_chunks - 1))
            # total chunks
            pchunks = chunks * len(self.parameters)
            num_pchunks = num_chunks * len(self.parameters)
            pparams = []
            param_set_ids = []
            presult_list = []
            for id, param in enumerate(self.parameters):
                param_set_ids.extend([id] * num_chunks)
                pparams.extend([param] * num_chunks)
                for i in range(num_chunks):
                    presult_list.append(
                        self.result_list[id][i * chunk_size:(i + 1) *
                                             chunk_size])

            results = self.lv.map_async(map_and_aggregate, presult_list,
                                        param_set_ids, [mapper] * num_pchunks,
                                        [aggregator] * num_pchunks,
                                        [cache_results] * num_pchunks)
        else:
            # If we don't store the realizations (or use the stored ones)
            if chunk_size is None:
                chunk_size = self._determine_chunk_size(number_of_trajectories)
            if not verbose:
                progress_bar = False
            else:
                print "Generating {0} realizations of the model, running mapper & aggregator (chunk size={1})".format(
                    number_of_trajectories, chunk_size)

            # chunks per parameter
            num_chunks = int(
                math.ceil(number_of_trajectories / float(chunk_size)))
            chunks = [chunk_size] * (num_chunks - 1)
            chunks.append(number_of_trajectories - chunk_size *
                          (num_chunks - 1))
            # total chunks
            pchunks = chunks * len(self.parameters)
            num_pchunks = num_chunks * len(self.parameters)
            pparams = []
            param_set_ids = []
            for id, param in enumerate(self.parameters):
                param_set_ids.extend([id] * num_chunks)
                pparams.extend([param] * num_chunks)

            seed_list = []
            for _ in range(len(self.parameters)):
                #need to do it this way cause the number of run per chunk might not be even
                seed_list.extend(
                    range(self.seed_base,
                          self.seed_base + number_of_trajectories, chunk_size))
                self.seed_base += number_of_trajectories
            #def run_ensemble_map_and_aggregate(model_class, parameters, seed_base, number_of_trajectories, mapper, aggregator=None):
            results = self.lv.map_async(run_ensemble_map_and_aggregate,
                                        [self.model_class] * num_pchunks,
                                        pparams, param_set_ids, seed_list,
                                        pchunks, [mapper] * num_pchunks,
                                        [aggregator] * num_pchunks)

        if progress_bar:
            # This should be factored out somehow.
            divid = str(uuid.uuid4())
            pb = HTML("""
                          <div style="border: 1px solid black; width:500px">
                          <div id="{0}" style="background-color:blue; width:0%">&nbsp;</div>
                          </div>
                          """.format(divid))
            display(pb)

        # We process the results as they arrive.
        mapped_results = {}
        for i, rset in enumerate(results):
            param_set_id = rset['param_set_id']
            r = rset['result']
            if param_set_id not in mapped_results:
                mapped_results[param_set_id] = []
            if type(r) is type([]):
                mapped_results[param_set_id].extend(
                    r)  #if a list is returned, extend that list
            else:
                mapped_results[param_set_id].append(r)
            if progress_bar:
                display(
                    Javascript("$('div#%s').width('%f%%')" %
                               (divid, 100.0 * (i + 1) / len(results))))

        if verbose:
            print "Running reducer on mapped and aggregated results (size={0})".format(
                len(mapped_results[0]))
        if reducer is None:
            reducer = builtin_reducer_default
        # Run reducer
        return self.run_reducer(reducer, mapped_results)
Пример #19
0
    def show_external_widget(self,
                             widget,
                             widget_title,
                             objects,
                             options,
                             auth_required=True):
        """
        Renders a JavaScript widget as loaded from a very simple hosted CDN.
        The CDN information is fetched dynamically from the local configuration.

        Parameters
        ----------
        widget: string or list
            If a string, should just be the name of the widget
            If a list, should be components on the versioned CDN path to that widget.
            E.g. "pairedEndLibrary" vs. ["widgets", "0.1.0", "pairedEndLibrary"]

        widget_title: string
            The title that appears in the header of the created widget.

        objects: dictionary
            This dict has the object information that feeds into the widget.

        options: dictionary
            This dict has widget-specific options used for rendering

        auth_required: boolean, default == True
            Whether or not authentication is required for fetching object data
        """
        #  Interface from Narrative's Python layer.
        #  The template placeholders will be substituted.
        #  widget_name - the registered widget name
        #  input_data - the expected input data (aka params) for the widget
        #  token - the current auth token, made available within the containing python function
        #  element - the output cell DOM node, as visible to this code due to the environment it is inserted into.
        #
        #  The Javascript functions doc from Jupyter:
        #  "When this object is returned by an expression or passed to the
        #  display function, it will result in the data being displayed
        #  in the frontend. If the data is a URL, the data will first be
        #  downloaded and then displayed.
        #
        #  In the Notebook, the containing element will be available as `element`,
        #  and jQuery will be available.  Content appended to `element` will be
        #  visible in the output area.""

        input_template = """
        element.html("<div id='{{input_id}}' class='kb-vis-area'>");

        require([
            'narrativeDataWidget'
        ], function (Jupyter, NarrativeDataWidget) {

            var widgetDef = JSON.parse('{{widget_def}}'),
                objectRefs = JSON.parse('{{object_refs}}'),
                options = JSON.parse('{{options}}'),
                config = JSON.parse('{{config}}'),
                packageName = widgetDef.package,
                packageVersion = widgetDef.package_version,
                widgetName = widgetDef.name,
                widgetParentNode = $('#{{input_id}}')[0];

            var dataWidget = NarrativeDataWidget.make({
                package: packageName,
                version: packageVersion,
                widget: widgetName,
                title: widgetDef.title,
                parent: widgetParentNode,
                authRequired: config.authRequired
            });

            dataWidget.runWidget(objectRefs, options)
                .then(function () {
                    console.log('FINISHED');
                })
                .catch(function (err) {
                    console.error('ERROR', err);
                    dataWidget.showErrorMessage(err.message);
                });
        });
        """

        # Prepare data for export into the Javascript.

        if type(widget) is list:
            widget_package = widget[0]
            widget_package_version = widget[1]
            widget_name = widget[2]
        else:
            widget_package = None
            widget_package_version = None
            widget_name = widget

        # Note: All Python->Javascript data flow is serialized as JSON strings.
        widget_def = {
            'id': self._cell_id_prefix + str(uuid.uuid4()),
            'package': widget_package,
            'package_version': widget_package_version,
            'name': widget_name,
            'title': widget_title
        }

        config = {'auth_required': auth_required}

        # context - Data for building the Javascript prior to insertion is provided
        # input_data - raw widget input data as provided by the caller
        js = Template(input_template).render(input_id=widget_def['id'],
                                             widget_def=json.dumps(widget_def),
                                             object_refs=json.dumps(objects),
                                             options=json.dumps(options),
                                             config=json.dumps(config))

        return Javascript(data=js, lib=None, css=None)
Пример #20
0
    def add_realizations(self,
                         number_of_trajectories=None,
                         chunk_size=None,
                         verbose=True,
                         progress_bar=True,
                         storage_mode="Shared"):
        """ Add a number of realizations to the ensemble. """
        if number_of_trajectories is None:
            raise MolnsUtilException("No number_of_trajectories specified")
        if type(number_of_trajectories) is not type(1):
            raise MolnsUtilException(
                "number_of_trajectories must be an integer")

        if chunk_size is None:
            chunk_size = self._determine_chunk_size(number_of_trajectories)

        if not verbose:
            progress_bar = False
        else:
            if len(self.parameters) > 1:
                print "Generating {0} realizations of the model at {1} parameter points (chunk size={2})".format(
                    number_of_trajectories, len(self.parameters), chunk_size)
            else:
                print "Generating {0} realizations of the model (chunk size={1})".format(
                    number_of_trajectories, chunk_size)

        self.number_of_trajectories += number_of_trajectories

        num_chunks = int(math.ceil(number_of_trajectories / float(chunk_size)))
        chunks = [chunk_size] * (num_chunks - 1)
        chunks.append(number_of_trajectories - chunk_size * (num_chunks - 1))
        # total chunks
        pchunks = chunks * len(self.parameters)
        num_pchunks = num_chunks * len(self.parameters)
        pparams = []
        param_set_ids = []
        for id, param in enumerate(self.parameters):
            param_set_ids.extend([id] * num_chunks)
            pparams.extend([param] * num_chunks)

        seed_list = []
        for _ in range(len(self.parameters)):
            #need to do it this way cause the number of run per chunk might not be even
            seed_list.extend(
                range(self.seed_base, self.seed_base + number_of_trajectories,
                      chunk_size))
            self.seed_base += number_of_trajectories
        results = self.lv.map_async(run_ensemble,
                                    [self.model_class] * num_pchunks, pparams,
                                    param_set_ids, seed_list, pchunks,
                                    [storage_mode] * num_pchunks)

        if progress_bar:
            # This should be factored out somehow.
            divid = str(uuid.uuid4())
            pb = HTML("""
                          <div style="border: 1px solid black; width:500px">
                          <div id="{0}" style="background-color:blue; width:0%">&nbsp;</div>
                          </div>
                          """.format(divid))
            display(pb)

        # We process the results as they arrive.
        for i, ret in enumerate(results):
            r = ret['filenames']
            param_set_id = ret['param_set_id']
            if param_set_id not in self.result_list:
                self.result_list[param_set_id] = []
            self.result_list[param_set_id].extend(r)
            if progress_bar:
                display(
                    Javascript("$('div#%s').width('%f%%')" %
                               (divid, 100.0 * (i + 1) / len(results))))

        return {
            'wall_time': results.wall_time,
            'serial_time': results.serial_time
        }
Пример #21
0
def test_function(leftgrid,rightgrid):

    display(Javascript("require.config({paths: {d3: 'https://d3js.org/d3.v5.min'}});"))
    display(Javascript(filename="./js/test_circle.js"))

    #   测试题目
    test_label = [widgets.Label(value = '1. 缓存(Cache)存在于__________'),
                  widgets.Label(value = '2. Cache的功能是________。'),
                  widgets.Label(value = '3. 以下关于Cache的叙述中,正确的是'),
                  widgets.Label(value = '4. 在主存和CPU之间增加Cache的目的是_______'),
                  widgets.Label(value = '5. 在计算机系统中,以下关于高速缓存 (Cache) 的说法正确的是( )'),
                  widgets.VBox([widgets.Label(value = '6. 在Cache的地址映射中,若主存中的任意一块均可映射到Cache内的任意一块的位置上,则这种方法称'),
                                widgets.Label(value = '   为________')]),
                  widgets.VBox([widgets.Label(value = '7. 计算机主存容量8MB,分为4096个主存块,cache有64KB,若按照采用直接映射方式,cache有'),
                                widgets.Label(value = '    ________块。')]),
                  widgets.VBox([widgets.Label(value = '8. 计算机主存容量8MB,分为4096个主存块,cache有64KB,若按照采用直接映射方式,Cache的字'),
                                widgets.Label(value = '   块内地址为________位。')]),
                  widgets.VBox([widgets.Label(value = '9. 计算机主存容量8MB,分为4096个主存块,cache有64KB,若按照采用直接映射方式,Cache的字'),
                                widgets.Label(value = '   块地址为________位。')]),
                  widgets.VBox([widgets.Label(value = '10. 一个组相联高速缓存由64个字块组成,每个字块有256字节,分为8组,主存有4096个字块。请问:'),
                                widgets.Label(value = '   主存地址有________位。')])]
    
    #   选项
    test1_A = widgets.HBox([widgets.Button(description = 'A',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '数据和信号',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test1_B = widgets.HBox([widgets.Button(description = 'B',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '内存和硬盘之间',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test1_C = widgets.HBox([widgets.Button(description = 'C',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '硬盘内部',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test1_D = widgets.HBox([widgets.Button(description = 'D',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = 'CPU内部',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    
    test2_A = widgets.HBox([widgets.Button(description = 'A',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '数值运算',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test2_B = widgets.HBox([widgets.Button(description = 'B',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '逻辑运算',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test2_C = widgets.HBox([widgets.Button(description = 'C',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '存储数据和指令',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test2_D = widgets.HBox([widgets.Button(description = 'D',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '控制程序',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])

    test3_A = widgets.HBox([widgets.Button(description = 'A',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '在容量确定的情况下,替换算法的时间复杂度是影响Cache命中率的关键因素',
                                           disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test3_B = widgets.HBox([widgets.Button(description = 'B',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = 'Cache的设计思想是在合理成本下提高命中率',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test3_C = widgets.HBox([widgets.Button(description = 'C',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = 'Cache的设计目标是容量尽可能与主存容量相等',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test3_D = widgets.HBox([widgets.Button(description = 'D',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = 'CPU中的Cache容量应大于CPU之外的Cache容量',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])

    test4_A = widgets.HBox([widgets.Button(description = 'A',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '扩大主存的容量',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test4_B = widgets.HBox([widgets.Button(description = 'B',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '增加CPU中通用寄存器的数量',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test4_C = widgets.HBox([widgets.Button(description = 'C',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '解决CPU和主存之间的速度匹配',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test4_D = widgets.HBox([widgets.Button(description = 'D',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '代替CPU中的寄存器工作',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])

    test5_A = widgets.HBox([widgets.Button(description = 'A',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = 'Cache的容量通常大于主存的存储容量',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test5_B = widgets.HBox([widgets.Button(description = 'B',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '通常由程序员设置Cache的内容和访问速度',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test5_C = widgets.HBox([widgets.Button(description = 'C',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = 'Cache 的内容是主存内容的副本',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])
    test5_D = widgets.HBox([widgets.Button(description = 'D',layout = widgets.Layout(width = '0.8cm',height = '0.7cm')),
                            widgets.Button(description = '多级Cache仅在多核cpu中使用',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.7cm'))])

    #    6-10题目文本输入框
    test6_text = widgets.Text()
    test7_text = widgets.Text()
    test8_text = widgets.Text()
    test9_text = widgets.Text()
    test10_text = widgets.Text()

        #   解析内容
    answer_label = [widgets.Label(value = '[解析] D。Cache介于CPU和内容之间的高速小容量存储器称为高速缓冲存储器,集成在CPU内部。'),
                    widgets.Label(value = '[解析] C。Cache的功能是存储数据和指令。'),
                    widgets.Label(value = '[解析] B。Cache是一个高速小容量的临时存储器,设计思想是提高命中率。'),
                    widgets.Label(value = '[解析] C。Cache提出来的目的就是解决CPU和主存之间速度不匹配的问题。'),
                    widgets.Label(value = '[解析] C。Cache 的内容是主存内容的副本。'),
                    widgets.Label(value = '[解析] 全相联映射:主存块可映射Cache的任意一块中。'),
                    widgets.Label(value = '[解析] 块容量 = 8MB / 4096 = 2KB, 64KB / 2B = 32个块'),
                    widgets.Label(value = '[解析] 块容量 = 8MB / 4096 = 2KB(11位)'),
                    widgets.Label(value = '[解析] Cache有32块(2的5次方),块地址5位'),
                    widgets.Label(value = '[解析] 主存容量4096 * 256 = 1MB(2的20次方),主存地址20位')]
    temp_label = widgets.Label()

    start = widgets.Button(description = '重做',layout = widgets.Layout(width = '2cm'))
    submit = widgets.Button(description = '提交',layout = widgets.Layout(width = '2cm'))

    test_out = []
    test_answer_option = [[test1_A,test1_B,test1_C,test1_D],
                          [test2_A,test2_B,test2_C,test2_D],
                          [test3_A,test3_B,test3_C,test3_D],
                          [test4_A,test4_B,test4_C,test4_D],
                          [test5_A,test5_B,test5_C,test5_D],
                          [test6_text],
                          [test7_text],
                          [test8_text],
                          [test9_text],
                          [test10_text]]

    for i in range(10):
        test_out.append(widgets.Output(layout = {'width':'auto','height':'auto'}))
        with test_out[i]:
            display(test_label[i])
            for j in range(len(test_answer_option[i])):
                display(test_answer_option[i][j])
                          
    for i in range(10):
        leftgrid[i,:] = test_out[i]
        
    leftgrid[10,1] = start
    leftgrid[10,3] = submit

    score_button_label = widgets.Button(description = '最新成绩',button_style = 'info',layout = widgets.Layout(width = 'auto',height = '0.8cm'))
    score_button = widgets.Button(description = '',disabled = False,layout = widgets.Layout(width = 'auto',height = '0.8cm'))

    condition_label = widgets.Button(description = '解题情况',button_style = 'info',disabled = True,layout = widgets.Layout(width = 'auto',height = '0.8cm'))

    condition = []
    
    for i in range(10):
        condition.append(TestCondition(test_index = str(i + 1)))

    rightgrid[0,0:2] = score_button_label
    rightgrid[0,2:6] = score_button
    rightgrid[2,1:5] = condition_label
    rightgrid[3,0] = condition[0]
    rightgrid[3,1] = condition[1]
    rightgrid[3,2] = condition[2]
    rightgrid[3,3] = condition[3]
    rightgrid[3,4] = condition[4]
    rightgrid[3,5] = condition[5]
    rightgrid[4,0] = condition[6]
    rightgrid[4,1] = condition[7]
    rightgrid[4,2] = condition[8]
    rightgrid[4,3] = condition[9]

    # 按钮事件
    def submit_click(b):
        score = 0
        for i in range(10):
            if condition[i].isRight == True:
                score = score + 10
                condition[i].color = '#F5F5F5'
            else:
                condition[i].color = 'red'
        score_button.description = str(score)

        for i in range(10):
            with test_out[i]:
                display(answer_label[i])

        for i in range(10):
            for j in range(len(test_answer_option[i])):
                if len(test_answer_option[i]) == 1:
                    test_answer_option[i][j].disabled = True
                else:
                    test_answer_option[i][j].children[0].disabled = True
        
    submit.on_click(submit_click)
    
    def start_click(b):
    
        for i in range(10):
            test_out[i].clear_output()
            
            for k in range(len(test_answer_option[i])):
                
                if len(test_answer_option[i]) == 1:
                    test_answer_option[i][k].disabled = False
                    test_answer_option[i][k].value = ''
                else:
                    test_answer_option[i][k].children[0].disabled = False
                    test_answer_option[i][k].children[0].style = {}
                    test_answer_option[i][k].children[0].button_style = ''
                    
            with test_out[i]:
                display(test_label[i])
                for j in range(len(test_answer_option[i])):
                    display(test_answer_option[i][j])

        for i in range(6):
            rightgrid[3,i].color = '#F5F5F5'
            rightgrid[3,i].isDo = False
            rightgrid[3,i].isRight = False
            if i < 4:
                rightgrid[4,i].color = '#F5F5F5'
                rightgrid[4,i].isDo = False
                rightgrid[4,i].isRight = False

        #score_button.description = ''
        
    start.on_click(start_click)
        
    def test1_A_click(b):  #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test1_B.children[0].button_style = ''
        test1_B.children[0].style = {}

        test1_C.children[0].button_style = ''
        test1_C.children[0].style = {}

        test1_D.children[0].button_style = ''
        test1_D.children[0].style = {}

        rightgrid[3,0].isDo = True
        rightgrid[3,0].isRight = False
        rightgrid[3,0].color = '#004080'
        
    test1_A.children[0].on_click(test1_A_click)

    def test1_B_click(b):  #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test1_A.children[0].button_style = ''
        test1_A.children[0].style = {}

        test1_C.children[0].button_style = ''
        test1_C.children[0].style = {}

        test1_D.children[0].button_style = ''
        test1_D.children[0].style = {}

        rightgrid[3,0].isDo = True
        rightgrid[3,0].isRight = False
        rightgrid[3,0].color = '#004080'
        
    test1_B.children[0].on_click(test1_B_click)

    def test1_C_click(b):  #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test1_B.children[0].button_style = ''
        test1_B.children[0].style = {}

        test1_A.children[0].button_style = ''
        test1_A.children[0].style = {}

        test1_D.children[0].button_style = ''
        test1_D.children[0].style = {}

        rightgrid[3,0].isDo = True
        rightgrid[3,0].isRight = False
        rightgrid[3,0].color = '#004080'
        
    test1_C.children[0].on_click(test1_C_click)

    def test1_D_click(b):  #对
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test1_A.children[0].button_style = ''
        test1_A.children[0].style = {}

        test1_C.children[0].button_style = ''
        test1_C.children[0].style = {}

        test1_B.children[0].button_style = ''
        test1_B.children[0].style = {}

        rightgrid[3,0].isDo = True
        rightgrid[3,0].isRight = True
        rightgrid[3,0].color = '#004080'
        
    test1_D.children[0].on_click(test1_D_click)

    def test2_A_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test2_D.children[0].button_style = ''
        test2_D.children[0].style = {}

        test2_C.children[0].button_style = ''
        test2_C.children[0].style = {}

        test2_B.children[0].button_style = ''
        test2_B.children[0].style = {}

        rightgrid[3,1].isDo = True
        rightgrid[3,1].isRight = False
        rightgrid[3,1].color = '#004080'
        
    test2_A.children[0].on_click(test2_A_click)

    def test2_B_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test2_A.children[0].button_style = ''
        test2_A.children[0].style = {}

        test2_C.children[0].button_style = ''
        test2_C.children[0].style = {}

        test2_D.children[0].button_style = ''
        test2_D.children[0].style = {}

        rightgrid[3,1].isDo = True
        rightgrid[3,1].isRight = False
        rightgrid[3,1].color = '#004080'
        
    test2_B.children[0].on_click(test2_B_click)

    def test2_C_click(b):   #对
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test2_A.children[0].button_style = ''
        test2_A.children[0].style = {}

        test2_D.children[0].button_style = ''
        test2_D.children[0].style = {}

        test2_B.children[0].button_style = ''
        test2_B.children[0].style = {}

        rightgrid[3,1].isDo = True
        rightgrid[3,1].isRight = True
        rightgrid[3,1].color = '#004080'
        
    test2_C.children[0].on_click(test2_C_click)

    def test2_D_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test2_A.children[0].button_style = ''
        test2_A.children[0].style = {}

        test2_C.children[0].button_style = ''
        test2_C.children[0].style = {}

        test2_B.children[0].button_style = ''
        test2_B.children[0].style = {}

        rightgrid[3,1].isDo = True
        rightgrid[3,1].isRight = False
        rightgrid[3,1].color = '#004080'
        
    test2_D.children[0].on_click(test2_D_click)

    def test3_A_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test3_D.children[0].button_style = ''
        test3_D.children[0].style = {}

        test3_C.children[0].button_style = ''
        test3_C.children[0].style = {}

        test3_B.children[0].button_style = ''
        test3_B.children[0].style = {}

        rightgrid[3,2].isDo = True
        rightgrid[3,2].isRight = False
        rightgrid[3,2].color = '#004080'
        
    test3_A.children[0].on_click(test3_A_click)

    def test3_B_click(b):   #对
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test3_A.children[0].button_style = ''
        test3_A.children[0].style = {}

        test3_C.children[0].button_style = ''
        test3_C.children[0].style = {}

        test3_D.children[0].button_style = ''
        test3_D.children[0].style = {}

        rightgrid[3,2].isDo = True
        rightgrid[3,2].isRight = True
        rightgrid[3,2].color = '#004080'
        
    test3_B.children[0].on_click(test3_B_click)

    def test3_C_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test3_A.children[0].button_style = ''
        test3_A.children[0].style = {}

        test3_D.children[0].button_style = ''
        test3_D.children[0].style = {}

        test3_B.children[0].button_style = ''
        test3_B.children[0].style = {}

        rightgrid[3,2].isDo = True
        rightgrid[3,2].isRight = False
        rightgrid[3,2].color = '#004080'
        
    test3_C.children[0].on_click(test3_C_click)

    def test3_D_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test3_A.children[0].button_style = ''
        test3_A.children[0].style = {}

        test3_C.children[0].button_style = ''
        test3_C.children[0].style = {}

        test3_B.children[0].button_style = ''
        test3_B.children[0].style = {}

        rightgrid[3,2].isDo = True
        rightgrid[3,2].isRight = False
        rightgrid[3,2].color = '#004080'
        
    test3_D.children[0].on_click(test3_D_click)

    def test4_A_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test4_D.children[0].button_style = ''
        test4_D.children[0].style = {}

        test4_C.children[0].button_style = ''
        test4_C.children[0].style = {}

        test4_B.children[0].button_style = ''
        test4_B.children[0].style = {}

        rightgrid[3,3].isDo = True
        rightgrid[3,3].isRight = False
        rightgrid[3,3].color = '#004080'
        
    test4_A.children[0].on_click(test4_A_click)

    def test4_B_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test4_A.children[0].button_style = ''
        test4_A.children[0].style = {}

        test4_C.children[0].button_style = ''
        test4_C.children[0].style = {}

        test4_D.children[0].button_style = ''
        test4_D.children[0].style = {}

        rightgrid[3,3].isDo = True
        rightgrid[3,3].isRight = False
        rightgrid[3,3].color = '#004080'
        
    test4_B.children[0].on_click(test4_B_click)

    def test4_C_click(b):   #对
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test4_A.children[0].button_style = ''
        test4_A.children[0].style = {}

        test4_D.children[0].button_style = ''
        test4_D.children[0].style = {}

        test4_B.children[0].button_style = ''
        test4_B.children[0].style = {}
        
        rightgrid[3,3].isDo = True
        rightgrid[3,3].isRight = True
        rightgrid[3,3].color = '#004080'
        
    test4_C.children[0].on_click(test4_C_click)

    def test4_D_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test4_A.children[0].button_style = ''
        test4_A.children[0].style = {}

        test4_C.children[0].button_style = ''
        test4_C.children[0].style = {}

        test4_B.children[0].button_style = ''
        test4_B.children[0].style = {}

        rightgrid[3,3].isDo = True
        rightgrid[3,3].isRight = False
        rightgrid[3,3].color = '#004080'
        
    test4_D.children[0].on_click(test4_D_click)

    def test5_A_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test5_D.children[0].button_style = ''
        test5_D.children[0].style = {}

        test5_C.children[0].button_style = ''
        test5_C.children[0].style = {}

        test5_B.children[0].button_style = ''
        test5_B.children[0].style = {}
        
        rightgrid[3,4].isDo = True
        rightgrid[3,4].isRight = False
        rightgrid[3,4].color = '#004080'

    test5_A.children[0].on_click(test5_A_click)

    def test5_B_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test5_A.children[0].button_style = ''
        test5_A.children[0].style = {}

        test5_C.children[0].button_style = ''
        test5_C.children[0].style = {}

        test5_D.children[0].button_style = ''
        test5_D.children[0].style = {}

        rightgrid[3,4].isDo = True
        rightgrid[3,4].isRight = False
        rightgrid[3,4].color = '#004080'
        
    test5_B.children[0].on_click(test5_B_click)

    def test5_C_click(b):   #对
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test5_A.children[0].button_style = ''
        test5_A.children[0].style = {}

        test5_D.children[0].button_style = ''
        test5_D.children[0].style = {}

        test5_B.children[0].button_style = ''
        test5_B.children[0].style = {}

        rightgrid[3,4].isDo = True
        rightgrid[3,4].isRight = True
        rightgrid[3,4].color = '#004080'
        
    test5_C.children[0].on_click(test5_C_click)

    def test5_D_click(b):   #错
        b.style = {'button_color':'#004080'}
        b.button_style = 'info'

        test5_A.children[0].button_style = ''
        test5_A.children[0].style = {}

        test5_C.children[0].button_style = ''
        test5_C.children[0].style = {}

        test5_B.children[0].button_style = ''
        test5_B.children[0].style = {}

        rightgrid[3,4].isDo = True
        rightgrid[3,4].isRight = False
        rightgrid[3,4].color = '#004080'
        
    test5_D.children[0].on_click(test5_D_click)

    def test6_text_change(change):    
        if change['new'] != '':
            rightgrid[3,5].isDo = True
            rightgrid[3,5].color = '#004080'
        else:
            rightgrid[3,5].isDo = False
            rightgrid[3,5].color = '#F5F5F5'

        if change['new'] == '全相联映射':
            rightgrid[3,5].isRight = True
        else:
            rightgrid[3,5].isRight = False
    test6_text.observe(test6_text_change,names = 'value')

    def test7_text_change(change):
        if change['new'] != '':
            rightgrid[4,0].isDo = True
            rightgrid[4,0].color = '#004080'
        else:
            rightgrid[4,0].isDo = False
            rightgrid[4,0].color = '#F5F5F5'

        if change['new'] == '32' or change['new'] == '32块' or change['new'] == '32个':
            rightgrid[4,0].isRight = True
        else:
            rightgrid[4,0].isRight = False
    test7_text.observe(test7_text_change,names = 'value')

    def test8_text_change(change):    
        if change['new'] != '':
            rightgrid[4,1].isDo = True
            rightgrid[4,1].color = '#004080'
        else:
            rightgrid[4,1].isDo = False
            rightgrid[4,1].color = '#F5F5F5'

        if change['new'] == '11' or change['new'] == '11位':
            rightgrid[4,1].isRight = True
        else:
            rightgrid[4,1].isRight = False
    test8_text.observe(test8_text_change,names = 'value')

    def test9_text_change(change):    
        if change['new'] != '':
            rightgrid[4,2].isDo = True
            rightgrid[4,2].color = '#004080'
        else:
            rightgrid[4,2].isDo = False
            rightgrid[4,2].color = '#F5F5F5'

        if change['new'] == '5' or change['new'] == '5位':
            rightgrid[4,2].isRight = True
        else:
            rightgrid[4,2].isRight = False
    test9_text.observe(test9_text_change,names = 'value')

    def test10_text_change(change):    
        if change['new'] != '':
            rightgrid[4,3].isDo = True
            rightgrid[4,3].color = '#004080'
        else:
            rightgrid[4,3].isDo = False
            rightgrid[4,3].color = '#F5F5F5'

        if change['new'] == '20' or change['new'] == '20位':
            rightgrid[4,3].isRight = True
        else:
            rightgrid[4,3].isRight = False
    test10_text.observe(test10_text_change,names = 'value')

    

    return
            os.path.join(project_folder, "src",
                         "conversation_analytics_toolkit", "transcript.css")))
    add_style_to_header(
        read_file(
            os.path.join(project_folder, "src",
                         "conversation_analytics_toolkit",
                         "wordpackchart.css")))
    add_style_to_header(
        read_file(
            os.path.join(project_folder, "src",
                         "conversation_analytics_toolkit",
                         "wa_dialog_chart.css")))

    display(
        Javascript(
            "require.config({paths: {d3: 'https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min'}});"
        ))
    display(
        Javascript(
            "require.config({paths: {lodash: 'https://cdn.jsdelivr.net/npm/[email protected]/lodash.min'}});"
        ))

    display(
        Javascript(filename=os.path.join(project_folder, "src",
                                         "conversation_analytics_toolkit",
                                         "svg_export.js")))

    display(
        Javascript(filename=os.path.join(project_folder, "src",
                                         "conversation_analytics_toolkit",
                                         "flowchart2.js")))
Пример #23
0
def take_photo(filename="photo.png", cam_width=None, cam_height=None):
    """
        take a photo using a camera. If the machine has multiple cameras,
        you need to switch the camera
        --- Usage ---
        from IPython.display import Image
        try:
            filename = take_photo(cam_width=256, cam_height=256) # better to pass the default resolutions
            print('Saved to {}'.format(filename))
            # Show the image which was just taken.
            display(Image(filename))
        except Exception as err:
            # Errors will be thrown if the user does not have a webcam or if they do not
            # grant the page permission to access it.
            print(str(err))
    """
    js = Javascript('''
    async function takePhoto(cam_width, cam_height) {
      // default config
      var CONSTRAINTS = {
        audio: false,
        video: {
          width: {ideal: cam_width},
          height: {ideal: cam_height},
          deviceId: null
        }
      };

      function syncCamera(video, dev_id){
        CONSTRAINTS.video.deviceId = dev_id;
        if(curSTREAM !== null){
          curSTREAM.getVideoTracks().forEach((camera) => {
            camera.stop();
          });
      }

      navigator.mediaDevices.getUserMedia(CONSTRAINTS)
        .then( (stream) => {
          curSTREAM = stream;
          video.srcObject = stream;
          video.onloadedmetadata = (e) => {
            video.play();
          };
        })
      .catch((err) => {
        //console.log(err.name + ": " + err.message);
        return;
        });
      }

      // current Stream
      var curSTREAM = null;

      const div = document.createElement('div');
      const capture = document.createElement('button');
      capture.textContent = 'Capture';
      div.appendChild(capture);
        if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
        //console.log("enumerateDevices() not supported.");
        return;
      }

      navigator.mediaDevices.enumerateDevices()
      .then(function(devices) {
        devices.forEach(function(device) {
          if (device.kind.includes("video")) {
            const dev_button = document.createElement('button');
            dev_button.textContent = device.label;
            const dev_id = device.deviceId;
            div.appendChild(dev_button);
            dev_button.onclick = function() {
            syncCamera(video, dev_id);
            }
          };
        });
      })
      .catch(function(err) {
        //console.log(err.name + ": " + err.message);
        return;
      });

      const video = document.createElement('video');
      video.style.display = 'block';
      curSTREAM = await navigator.mediaDevices.getUserMedia(CONSTRAINTS);
      document.body.appendChild(div);
      div.appendChild(video);
      video.srcObject = curSTREAM;
      await video.play();

      // Resize the output to fit the video element.
      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);

      // Wait for Capture to be clicked.
      await new Promise((resolve) => capture.onclick = resolve);

      const canvas = document.createElement('canvas');
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      canvas.getContext('2d').drawImage(video, 0, 0);
      curSTREAM.getVideoTracks()[0].stop();
      div.remove();
      return canvas.toDataURL();
      }
    ''')

    if not cam_width:
        cam_width = "null"
    if not cam_height:
        cam_height = "null"

    display(js)
    data = eval_js(f'takePhoto({cam_width}, {cam_height})')
    binary = b64decode(data.split(',')[1])
    with open(filename, 'wb') as f:
        f.write(binary)
    return filename
Пример #24
0
from IPython.display import HTML, Javascript

Javascript(f'window.names={names};')

%%javascript
require.config({paths: {vue: "https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.0/vue"}});

require(["vue"], function(Vue) {
  window.app = new Vue({
    el: '#app',
    data: {
      names: names,
    },
  })
})

%%html
<div id="app">
  <div v-for="name in ['test']">
    {{ name }}
  </div>
</div>
Пример #25
0
def _move_notebook_to_the_right():
    script_template = """
    var x = $('#notebook-container');
    x.css({position: "relative", left: "20%"});
    """
    display(Javascript(script_template))
Пример #26
0
def draw_bbox(image_urls, callbackId):  # pylint: disable=invalid-name
  """Open the bounding box UI and send the results to a callback function.

  Args:
    image_urls: list[str | np.ndarray]
      List of locations from where to load the images from. If a np.ndarray is
      given, the array is interpretted as an image and sent to the frontend.
      If a str is given, the string is interpreted as a path and is read as a
      np.ndarray before being sent to the frontend.

    callbackId: str
      The ID for the callback function to send the bounding box results to
      when the user hits submit.
  """
  js = Javascript('''
              async function load_image(imgs, callbackId) {
                  //init organizational elements
                  const div = document.createElement('div');
                  var image_cont = document.createElement('div');
                  var errorlog = document.createElement('div');
                  var crosshair_h = document.createElement('div');
                  crosshair_h.style.position = "absolute";
                  crosshair_h.style.backgroundColor = "transparent";
                  crosshair_h.style.width = "100%";
                  crosshair_h.style.height = "0px";
                  crosshair_h.style.zIndex = 9998;
                  crosshair_h.style.borderStyle = "dotted";
                  crosshair_h.style.borderWidth = "2px";
                  crosshair_h.style.borderColor = "rgba(255, 0, 0, 0.75)";
                  crosshair_h.style.cursor = "crosshair";
                  var crosshair_v = document.createElement('div');
                  crosshair_v.style.position = "absolute";
                  crosshair_v.style.backgroundColor = "transparent";
                  crosshair_v.style.width = "0px";
                  crosshair_v.style.height = "100%";
                  crosshair_v.style.zIndex = 9999;
                  crosshair_v.style.top = "0px";
                  crosshair_v.style.borderStyle = "dotted";
                  crosshair_v.style.borderWidth = "2px";
                  crosshair_v.style.borderColor = "rgba(255, 0, 0, 0.75)";
                  crosshair_v.style.cursor = "crosshair";
                  crosshair_v.style.marginTop = "23px";
                  var brdiv = document.createElement('br');


                  //init control elements
                  var next = document.createElement('button');
                  var prev = document.createElement('button');
                  var submit = document.createElement('button');
                  var deleteButton = document.createElement('button');
                  var deleteAllbutton = document.createElement('button');

                  //init image containers
                  var image = new Image();
                  var canvas_img = document.createElement('canvas');
                  var ctx = canvas_img.getContext("2d");
                  canvas_img.style.cursor = "crosshair";
                  canvas_img.setAttribute('draggable', false);
                  crosshair_v.setAttribute('draggable', false);
                  crosshair_h.setAttribute('draggable', false);

                  // bounding box containers
                  const height = 600
                  var allBoundingBoxes = [];
                  var curr_image = 0
                  var im_height = 0;
                  var im_width = 0;

                  //initialize bounding boxes
                  for (var i = 0; i < imgs.length; i++) {
                    allBoundingBoxes[i] = [];
                  }
                  //initialize image view
                  errorlog.id = 'errorlog';
                  image.style.display = 'block';
                  image.setAttribute('draggable', false);

                  //load the first image
                  img = imgs[curr_image];
                  image.src = "data:image/png;base64," + img;
                  image.onload = function() {
                      // normalize display height and canvas
                      image.height = height;
                      image_cont.height = canvas_img.height = image.height;
                      image_cont.width = canvas_img.width = image.naturalWidth;
                      crosshair_v.style.height = image_cont.height + "px";
                      crosshair_h.style.width = image_cont.width + "px";

                      // draw the new image
                      ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0,  canvas_img.width,  canvas_img.height);

                  };

                  // move to next image in array
                  next.textContent = "next image";
                  next.onclick = function(){
                      if (curr_image < imgs.length - 1){
                          // clear canvas and load new image
                          curr_image += 1;
                          errorlog.innerHTML = "";
                      }
                      else{
                          errorlog.innerHTML = "All images completed!!";
                      }
                      resetcanvas();
                  }

                  //move forward through list of images
                  prev.textContent = "prev image"
                  prev.onclick = function(){
                      if (curr_image > 0){
                          // clear canvas and load new image
                          curr_image -= 1;
                          errorlog.innerHTML = "";
                      }
                      else{
                          errorlog.innerHTML = "at the beginning";
                      }
                      resetcanvas();
                  }
                  // on delete, deletes the last bounding box
                  deleteButton.textContent = "undo bbox";
                  deleteButton.onclick = function(){
                    boundingBoxes.pop();
                    ctx.clearRect(0, 0, canvas_img.width, canvas_img.height);
                    image.src = "data:image/png;base64," + img;
                    image.onload = function() {
                        ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0,  canvas_img.width,  canvas_img.height);
                        boundingBoxes.map(r => {drawRect(r)});
                    };
                  }
                  // on all delete, deletes all of the bounding box
                  deleteAllbutton.textContent = "delete all"
                  deleteAllbutton.onclick = function(){
                    boundingBoxes = [];
                    ctx.clearRect(0, 0, canvas_img.width, canvas_img.height);
                    image.src = "data:image/png;base64," + img;
                    image.onload = function() {
                        ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0,  canvas_img.width,  canvas_img.height);
                        //boundingBoxes.map(r => {drawRect(r)});
                    };
                  }

                  // on submit, send the boxes to display
                  submit.textContent = "submit";
                  submit.onclick = function(){
                    errorlog.innerHTML = "";

                    // send box data to callback fucntion
                    google.colab.kernel.invokeFunction(callbackId, [allBoundingBoxes], {});
                  }

                // init template for annotations
                const annotation = {
                      x: 0,
                      y: 0,
                      w: 0,
                      h: 0,
                };

                // the array of all rectangles
                let boundingBoxes = allBoundingBoxes[curr_image];

                // the actual rectangle, the one that is being drawn
                let o = {};

                // a variable to store the mouse position
                let m = {},

                // a variable to store the point where you begin to draw the
                // rectangle
                start = {};

                // a boolean variable to store the drawing state
                let isDrawing = false;
                var elem = null;

                function handleMouseDown(e) {
                  // on mouse click set change the cursor and start tracking the mouse position
                  start = oMousePos(canvas_img, e);

                  // configure is drawing to true
                  isDrawing = true;
                }

                function handleMouseMove(e) {
                    // move crosshairs, but only within the bounds of the canvas
                    if (document.elementsFromPoint(e.pageX, e.pageY).includes(canvas_img)) {
                      crosshair_h.style.top = e.pageY + "px";
                      crosshair_v.style.left = e.pageX + "px";
                    }

                    // move the bounding box
                    if(isDrawing){
                      m = oMousePos(canvas_img, e);
                      draw();
                    }
                }

                function handleMouseUp(e) {
                    if (isDrawing) {
                        // on mouse release, push a bounding box to array and draw all boxes
                        isDrawing = false;

                        const box = Object.create(annotation);

                        // calculate the position of the rectangle
                        if (o.w > 0){
                          box.x = o.x;
                        }
                        else{
                          box.x = o.x + o.w;
                        }
                        if (o.h > 0){
                          box.y = o.y;
                        }
                        else{
                          box.y = o.y + o.h;
                        }
                        box.w = Math.abs(o.w);
                        box.h = Math.abs(o.h);

                        // add the bounding box to the image
                        boundingBoxes.push(box);
                        draw();
                    }
                }

                function draw() {
                    o.x = (start.x)/image.width;  // start position of x
                    o.y = (start.y)/image.height;  // start position of y
                    o.w = (m.x - start.x)/image.width;  // width
                    o.h = (m.y - start.y)/image.height;  // height

                    ctx.clearRect(0, 0, canvas_img.width, canvas_img.height);
                    ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0,  canvas_img.width,  canvas_img.height);
                    // draw all the rectangles saved in the rectsRy
                    boundingBoxes.map(r => {drawRect(r)});
                    // draw the actual rectangle
                    drawRect(o);
                }

                // add the handlers needed for dragging
                crosshair_h.addEventListener("mousedown", handleMouseDown);
                crosshair_v.addEventListener("mousedown", handleMouseDown);
                document.addEventListener("mousemove", handleMouseMove);
                document.addEventListener("mouseup", handleMouseUp);


                function resetcanvas(){
                    // clear canvas
                    ctx.clearRect(0, 0, canvas_img.width, canvas_img.height);
                    img = imgs[curr_image]
                    image.src = "data:image/png;base64," + img;

                    // onload init new canvas and display image
                    image.onload = function() {
                        // normalize display height and canvas
                        image.height = height;
                        image_cont.height = canvas_img.height = image.height;
                        image_cont.width = canvas_img.width = image.naturalWidth;
                        crosshair_v.style.height = image_cont.height + "px";
                        crosshair_h.style.width = image_cont.width + "px";

                        // draw the new image
                        ctx.drawImage(image, 0, 0, image.naturalWidth, image.naturalHeight, 0, 0,  canvas_img.width,  canvas_img.height);

                        // draw bounding boxes
                        boundingBoxes = allBoundingBoxes[curr_image];
                        boundingBoxes.map(r => {drawRect(r)});
                    };
                }

                function drawRect(o){
                    // draw a predefined rectangle
                    ctx.strokeStyle = "red";
                    ctx.lineWidth = 2;
                    ctx.beginPath(o);
                    ctx.rect(o.x * image.width, o.y * image.height, o.w * image.width, o.h * image.height);
                    ctx.stroke();
                }

                // Function to detect the mouse position
                function oMousePos(canvas_img, evt) {
                  let ClientRect = canvas_img.getBoundingClientRect();
                    return {
                      x: evt.clientX - ClientRect.left,
                      y: evt.clientY - ClientRect.top
                    };
                }


                //configure colab output display
                google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);

                //build the html document that will be seen in output
                div.appendChild(document.createElement('br'))
                div.appendChild(image_cont)
                image_cont.appendChild(canvas_img)
                image_cont.appendChild(crosshair_h)
                image_cont.appendChild(crosshair_v)
                div.appendChild(document.createElement('br'))
                div.appendChild(errorlog)
                div.appendChild(prev)
                div.appendChild(next)
                div.appendChild(deleteButton)
                div.appendChild(deleteAllbutton)
                div.appendChild(document.createElement('br'))
                div.appendChild(brdiv)
                div.appendChild(submit)
                document.querySelector("#output-area").appendChild(div);
                return
            }''')

  # load the images as a byte array
  bytearrays = []
  for image in image_urls:
    if isinstance(image, np.ndarray):
      bytearrays.append(image_from_numpy(image))
    else:
      raise TypeError('Image has unsupported type {}.'.format(type(image)))

  # format arrays for input
  image_data = json.dumps(bytearrays)
  del bytearrays

  # call java script function pass string byte array(image_data) as input
  display(js)
  eval_js('load_image({}, \'{}\')'.format(image_data, callbackId))
  return
Пример #27
0
Файл: nbx.py Проект: sutt/nbx
def receive_answer( debug=False,):
    '''
         like `nbx.receive_answer` but:
            - all in js
            - less arguments / options
    '''

    js = '''
    console.log("ra: 0th")
    Promise.resolve(
        // console.log("ra: 1st")
        IPython.notebook.save_notebook(true)
        ).then(function(){
            console.log("ra: 2nd")
            return theFinal();
            }
        ).then(function(){
            // return theRest();
        });

    function theFinal() {
        
        var nb_name = IPython.notebook.notebook_name;
        const t0 = new Date().getTime()

        const simple_cb = {shell:{reply: () => {
                const t1 = new Date().getTime()
                console.log("merge() has finished! time in ms: " + (t1 - t0))
                theRest()
            }
            }}
        
        var py_cmd = '';
        py_cmd += 'nbx.ImportClass.gitcomm_pull_answer()';
        IPython.notebook.kernel.execute(py_cmd);
        console.log(py_cmd);

        var py_cmd = '';
        py_cmd += 'nbx.ImportClass.merge_get_answer("' + nb_name + '")';
        IPython.notebook.kernel.execute(py_cmd, simple_cb);
        console.log(py_cmd);

        }

    function theRest() {

    
    var nb_path = IPython.notebook.notebook_path;
    var cell_index = IPython.notebook.get_selected_index() - 1;

    var b_log = true;
    var b_save = false;
    var b_scroll = true;
    var b_select = true;
    var b_flash = true;

    
    // setTimeout(loadFunc, 5000);
    loadFunc()

    function loadFunc() {
        IPython.notebook.load_notebook(nb_path);
        console.log("loadFunc() has run")
    }
    if (b_log) {console.log('after load');}
    
    setTimeout(basicFunc, 300);  // still needs a delay to allow the reload to finish; could move to cb architecutre later
    // basicFunc()
    function basicFunc() {
        
        console.log('in basicFunc');   
        var orig_cell = IPython.notebook.get_cell(cell_index);
        var html_cell = $(orig_cell.element)[0];
        
        if (b_scroll) {
        $(orig_cell.element)[0].scrollIntoViewIfNeeded({inline:'center'});}
        
        if (b_select) {IPython.notebook.select(cell_index);}
        
        function flash(ms_flash) {
            Promise.resolve(
                $(html_cell).stop().animate({backgroundColor:'#008000'}, ms_flash).promise()
                ).then(function(){
                    return $(html_cell).stop().animate(
                        {backgroundColor:'#FFFFFF'}, ms_flash);
                    }
                );
        }
        if (b_flash) {flash(500);}
        
        if (b_log) {console.log('end of basicFunc')}
    } // end basicFunc ------------
    
    if (b_log) {console.log('done with theRest()');}
    } // end theRest---------------
    
    '''

    js = '\n'.join([line.strip() for line in js.split('\n') if line.strip !=''])
    
    if debug:
        try:
            with open('js-debug-nbx.js', 'w') as f:
                f.write(js)
        except:
            print('debug option set True, but failed to output js to `js-debug.js. directory might not exist?')

    display(Javascript(js))
Пример #28
0
    def display(self):
        from IPython.core.display import display, Javascript

        return display(Javascript(self._repr_javascript_()))
Пример #29
0
 def grade_blockpy(self, line=""):
     dump = self.grade_parser(line)
     code = self.unified_helper(BLOCKPY_GRADE,
                                assignment=dump["assignment"],
                                inputs=dump["inputs"])
     return display(Javascript(code))
Пример #30
0
def preview(image, **kwargs):
    try:
        from IPython.display import Javascript, HTML, display
        from gbdxtools import Interface
        gbdx = Interface()
    except:
        print("IPython is required to produce maps.")
        return

    zoom = kwargs.get("zoom", 16)
    bands = kwargs.get("bands", image._rgb_bands)
    wgs84_bounds = kwargs.get(
        "bounds",
        list(loads(image.ipe_metadata["image"]["imageBoundsWGS84"]).bounds))
    center = kwargs.get("center", list(shape(image).centroid.bounds[0:2]))
    graph_id = image.ipe_id
    node_id = image.ipe.graph()['nodes'][0]['id']

    # fetch a tile in order to calc stats and do a simple stretch
    y = image.shape[1] / 2
    x = image.shape[2] / 2
    aoi = image[:, y:y + 256, x:x + 256].read(quiet=True)
    means, stds = aoi.mean(axis=(1, 2)), aoi.std(axis=(1, 2))
    scales = (255.0 / (4.0 * stds))
    offsets = map(
        list(((means - (2.0 * stds)) * scales * -1.0)).__getitem__, bands)
    scales = map(list(scales).__getitem__, bands)

    if image.proj != 'EPSG:4326':
        code = image.proj.split(':')[1]
        conn = gbdx.gbdx_connection
        proj_info = conn.get(
            'https://ughlicoordinates.geobigdata.io/ughli/v1/projinfo/{}'.
            format(code)).json()
        tfm = partial(pyproj.transform, pyproj.Proj(init='EPSG:4326'),
                      pyproj.Proj(init=image.proj))
        bounds = list(ops.transform(tfm, box(*wgs84_bounds)).bounds)
    else:
        proj_info = {}
        bounds = wgs84_bounds

    map_id = "map_{}".format(str(int(time.time())))
    display(
        HTML(
            Template('''
       <div id="$map_id"/>
       <link href='https://openlayers.org/en/v4.6.4/css/ol.css' rel='stylesheet' />
       <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
       <style>body{margin:0;padding:0;}#$map_id{position:relative;top:0;bottom:0;width:100%;height:400px;}</style>
       <style></style>
    ''').substitute({"map_id": map_id})))

    js = Template("""
        require.config({
            paths: {
                ol: 'https://openlayers.org/en/v4.6.4/build/ol',
                proj4: 'https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.4.4/proj4'
            }
        });

        require(['ol', 'proj4'], function(ol, proj4) {
            ol.proj.setProj4(proj4);
            var md = $md;
            var georef = $georef;
            var graphId = '$graphId';
            var nodeId = '$nodeId';
            var extents = $bounds; 

            var x1 = md.minTileX * md.tileXSize;
            var y1 = ((md.minTileY + md.numYTiles) * md.tileYSize + md.tileYSize);
            var x2 = ((md.minTileX + md.numXTiles) * md.tileXSize + md.tileXSize);
            var y2 = md.minTileY * md.tileYSize;
            var tileLayerResolutions = [georef.scaleX];

            var url = '$url' + '/tile/';
            url += graphId + '/' + nodeId;
            url += "/{x}/{y}.png?token=$token&bands=$bands&scales=$scales&offsets=$offsets";

            var proj = '$proj';
            var projInfo = $projInfo;
    
            if ( proj !== 'EPSG:4326' ) {
                var proj4def = projInfo["proj4"];
                proj4.defs(proj, proj4def);
                var area = projInfo["area_of_use"];
                var bbox = [area["area_west_bound_lon"], area["area_south_bound_lat"], 
                            area["area_east_bound_lon"], area["area_north_bound_lat"]]
                var projection = ol.proj.get(proj);
                var fromLonLat = ol.proj.getTransform('EPSG:4326', projection);
                var extent = ol.extent.applyTransform(
                    [bbox[0], bbox[1], bbox[2], bbox[3]], fromLonLat);
                projection.setExtent(extent);
            } else {
                var projection = ol.proj.get(proj);
            }

            var rda = new ol.layer.Tile({
              title: 'RDA',
              opacity: 1,
              extent: extents,
              source: new ol.source.TileImage({
                      crossOrigin: null,
                      projection: projection,
                      extent: extents,

                      tileGrid: new ol.tilegrid.TileGrid({
                          extent: extents,
                          origin: [extents[0], extents[3]],
                          resolutions: tileLayerResolutions,
                          tileSize: [md.tileXSize, md.tileYSize],
                      }),
                      tileUrlFunction: function (coordinate) {
                          if (coordinate === null) return undefined;
                          const x = coordinate[1] + md.minTileX;
                          const y = -(coordinate[2] + 1 - md.minTileY);
                          if (x < md.minTileX || x > md.maxTileX) return undefined;
                          if (y < md.minTileY || y > md.maxTileY) return undefined;
                          return url.replace('{x}', x).replace('{y}', y);
                      }
                  })
            });

            var map = new ol.Map({
              layers: [ rda ],
              target: '$map_id',
              view: new ol.View({
                projection: projection,
                center: $center,
                zoom: $zoom
              })
            });
        });
    """).substitute({
        "map_id": map_id,
        "proj": image.proj,
        "projInfo": json.dumps(proj_info),
        "graphId": graph_id,
        "bounds": bounds,
        "bands": ",".join(map(str, bands)),
        "nodeId": node_id,
        "md": json.dumps(image.ipe_metadata["image"]),
        "georef": json.dumps(image.ipe_metadata["georef"]),
        "center": center,
        "zoom": zoom,
        "token": gbdx.gbdx_connection.access_token,
        "scales": ",".join(map(str, scales)),
        "offsets": ",".join(map(str, offsets)),
        "url": VIRTUAL_IPE_URL
    })
    display(Javascript(js))