Пример #1
0
def setup_comm_colab(api_call_id, callback):
    """Function that connects javascript call with a Colab Notebook"""
    from google.colab import output
    from IPython import display
    def _recv(msg):
        return display.JSON(callback(msg)) # Use display.JSON to transfer an object
    output.register_callback(api_call_id, _recv)
Пример #2
0
    def add_event_listener(self, name, callback):
        """Adds an event listener to the element.

    Args:
      name: Name of the event.
      callback: The python function or js string to evaluate when event occurs.
    Raises:
      ValueError: If callback is not valid.
    """
        msg = {'name': name}
        if isinstance(callback, six.string_types):
            callbacks = self._js_listeners.get(name, {})
            if callback in callbacks:
                raise ValueError('Callback is already added.')
            callbacks[callback] = callback
            self._js_listeners[name] = callbacks
            msg['method'] = 'addJsEventListener'
            msg['value'] = callback
        elif callable(callback):
            callbacks = self._py_listeners.get(name, {})
            if callback in callbacks:
                raise ValueError('Callback is already added.')
            callback_name = str(uuid.uuid4())
            output.register_callback(callback_name, callback)
            callbacks[callback] = callback_name
            self._py_listeners[name] = callbacks
            msg['method'] = 'addPythonEventListener'
            msg['value'] = callback_name
        else:
            raise ValueError('callback must be a js string or callable python')
        if self._exists():
            _proxy(self._guid, msg)
Пример #3
0
    def register_effect(self, name, effect):
        output.register_callback(name, effect)

        def effect_function(*args, **kwargs):
            return f'''{{google.colab.kernel.invokeFunction('{name}', [], {{}})}}'''

        return effect_function
Пример #4
0
def register_colab_handler(message: str,
                           callback: Callable):  # pragma: no cover
    """Register a given callable to handle incomming requests for the given
    message name in a Colab Notebook environment. This function raises a
    NameError if called outside of a Colab Notebook environment.

    Parameters
    ----------
    message: string
        Unique message name (identifier).
    callback: callable
        Handler that is called on incomming messages. The message data will be
        passed to the handler as the only argument.

    Raises
    ------
    NameError
    """
    # Function that connects javascript call with Colab Notebook
    from google.colab import output
    from IPython import display

    def _recv(msg):
        # Call the given callback handler with the message data and send
        # the returned response.
        return display.JSON(
            callback(msg))  # Use display.JSON to transfer an object

    output.register_callback(message, _recv)
    def __init__(self, moveLabels, line_breaks=False):
        """
        :param moveLabels: make sure it is a collections.OrderedDict()
        """
        self.moveClass="UIBOXMOVE"
        self.removeClass="UIBOXREMOVE"
        self.uiMoveLables=moveLabels
        self.line_breaks = line_breaks

        output.register_callback('callback_letsdoit', self.cbTriggered)
 def _repr_html_(self):
   callback_id = 'button-' + str(uuid.uuid4())
   output.register_callback(callback_id, self._callback)
   template = """<button class="p-Widget jupyter-widgets jupyter-button widget-button mod-info" id="{callback_id}">{title}</button>
       <script>
         document.querySelector("#{callback_id}").onclick = (e) => {{
           google.colab.kernel.invokeFunction('{callback_id}', [], {{}})
           e.preventDefault();
         }};
       </script>"""
   html = template.format(title=self._title, callback_id=callback_id)
   return html
Пример #7
0
def ask(question_name):

    html_str = get_html(question_name)
    javascript_str = get_javascript(question_name, ENV)
    display(HTML(html_str + javascript_str))

    # register js-to-py callback for Google Colab notebooks
    if ENV == "google-colab":
        from google.colab.output import register_callback, eval_js
        register_callback( \
         question_name+"-id", \
         lambda: record_answer(question_name, eval_js('lgAns'), answers_obj))
Пример #8
0
def register_callback(callback_id, callback):
    def the_callback(*args, **kwargs):
        try:
            callback(*args, **kwargs)
        except Exception as err:
            traceback.print_exc()
            print('Error: ', err)
    if vdomr_global['mode'] == 'colab':
        from google.colab import output as colab_output
        colab_output.register_callback(callback_id, the_callback)
        exec_javascript(
            'window.vdomr_invokeFunction=google.colab.kernel.invokeFunction')
    elif (vdomr_global['mode'] == 'jp_proxy_widget') or (vdomr_global['mode'] == 'server') or (vdomr_global['mode'] == 'pyqt5'):
        vdomr_global['invokable_functions'][callback_id] = the_callback
Пример #9
0
    def _repr_html_(self):
        callback_id = 'button-' + str(uuid.uuid4())
        output.register_callback(callback_id, self._callback)

        template = """<button id="{callback_id}" style="height:3cm;">{title}</button>
        <script>
          document.querySelector("#{callback_id}").onclick = (e) => {{
            //IPython.notebook.execute_cells_after()
            google.colab.kernel.invokeFunction('{callback_id}', [], {{}})
            e.preventDefault();
          }};
        </script>"""
        html = template.format(title=self._title, callback_id=callback_id)
        return html
Пример #10
0
  def _repr_html_(self):
    callback_id = 'input-' + str(uuid.uuid4())
    output.register_callback(callback_id, self._callback)

    template = """{title}
      <input type="text" id={callback_id} value="ex: 1-405"></input>
        <script>
          document.querySelector("#{callback_id}").onchange = (e) => {{
            google.colab.kernel.invokeFunction('{callback_id}', [e.target.value], {{}})
            e.preventDefault();
          }};
        </script>"""
    html = template.format(title=self._title, callback_id=callback_id)
    return html 
Пример #11
0
  def _repr_html_(self):
    from google.colab import output
    callback_id = 'button-' + str(uuid.uuid4())
    output.register_callback(callback_id, self._callback)

    template = """<button id="{callback_id}" style="cursor:pointer;background-color:#EEEEEE;border-color:#E0E0E0;padding:5px 15px;font-size:14px">{title}</button>
        <script>
          document.querySelector("#{callback_id}").onclick = (e) => {{
            google.colab.kernel.invokeFunction('{callback_id}', [], {{}})
            e.preventDefault();
          }};
        </script>"""
    html = template.format(title=self._title, callback_id=callback_id)
    return html
Пример #12
0
def Display(GenerateMethod, val=None):
    '''Метод для запуска чатбота.
  
  ::param::GenerateMethod - любой метод, принимающий на вход последнее высказывание диалога, а возвращающее свое высказывание в str.
  
  '''
    display(IPython.display.HTML(chatbot_html + \
                               "<script>let getResp = colabGetResp;</script>"))

    def get_response(val):
        resp = GenerateMethod(val)
        return IPython.display.JSON({'result': resp})

    output.register_callback('notebook.get_response', get_response)
Пример #13
0
    def __init__(self,
                 signaling_folder=None,
                 webrtc_server=None,
                 room=None,
                 javacript_callable=False):
        if room is None:
            room = "".join([random.choice("0123456789") for x in range(10)])

        if webrtc_server is None and signaling_folder is None:
            raise ValueError(
                'Either a WebRTC server or a signaling folder must be provided.'
            )
        if webrtc_server is None:
            self._webrtc_server = FilesystemRTCServer(folder=signaling_folder)
        else:
            self._webrtc_server = webrtc_server

        self._room = room
        self._javascript_callable = javacript_callable

        if output and javacript_callable:
            output.register_callback(f'{room}.colab.signaling.connect',
                                     self.connect_sync)
            output.register_callback(f'{room}.colab.signaling.send',
                                     self.send_sync)
            output.register_callback(f'{room}.colab.signaling.receive',
                                     self.receive_sync)
            output.register_callback(f'{room}.colab.signaling.close',
                                     self.close_sync)
Пример #14
0
def draw_synthetic_input(input_height, input_width, callback):
    """ Draw synthetic input and call the provided callback
        with the image array when image is changed.

    Args:
        input_height ([int]): height of the input image
        input_width ([int]): width of the input image
        callback ([func]): callback to call when drawn image is changed.
            The only argument to the callback will be a 2D image_array
            with grayscale values of type int within [0, 255]
    """
    def draw_synthetic_input_callback(x):
        x = json.loads(x)  # parse json from string
        callback(x)
        return ipd.JSON({'result': "parsed synthetic input"})

    # JS to PY communication.
    # See https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=Ytn7tY-C9U0T
    output_callback_name = "draw_synthetic_input_callback_" + str(
        randrange(1000))
    output.register_callback('notebook.' + output_callback_name,
                             draw_synthetic_input_callback)

    dlight.load_js_libs()
    utils_dir = osp.dirname(osp.realpath(__file__))
    ipd.display(
        ipd.Javascript(filename=osp.join(utils_dir, "js", "draw_image.js")))
    ipd.display(
        ipd.HTML(filename=osp.join(utils_dir, "js", "draw_image.css.html")))

    data = {
        "image_height": input_height,
        "image_width": input_width,
        "callback_name": output_callback_name
    }

    container_id = "draw-image-container-" + str(randrange(1000))
    ipd.display(ipd.HTML("<div id='{}'></div> ".format(container_id)))
    ipd.display(
        ipd.Javascript("""
            require(['draw_image'], function(draw_image) {{
                draw_image(document.getElementById("{}"), {});
            }});
        """.format(container_id, json.dumps(data))))
Пример #15
0
    def create_labeled_variables(self, label_dict):
        """Inserts labeled entries into a dictonary that can be used in colab.

        This function takes a dictionary as input. It extracts labeled examples
        stored in localStorage and inserts them into a dictonary.
        """
        self.label_dict = label_dict
        output.register_callback('notebook.AddListItem', self.add_list_item)
        command = """
                for (var i=0; i<localStorage.length; i++) {
                      var key = localStorage.key(i);
                      var existingItem = localStorage.getItem(key);
                      var var_name = key;
                      if (key === null) {
                          continue
                      }
                      google.colab.kernel.invokeFunction('notebook.AddListItem', [key, existingItem], {});
              }
              """
        return Javascript(command)
Пример #16
0
def init(moveLabels={}):
    """
    :param moveLabels: make sure it is a collections.OrderedDict()
    :return:
    """
    print("Init checkboxmulti")

    output.register_callback('callback_letsdoit', cbTriggered)

    global uiMoveLables
    uiMoveLables = moveLabels

    cssCode = css(removeClass, '#F00')

    for lbl, __ in uiMoveLables.items():
        #cssCode += css(moveClass + lbl, "#090")
        mecolor = anyTextToColor(lbl)
        cssCode += css(moveClass + lbl, "#" + mecolor)

    return cssCode
Пример #17
0
    def _repr_html_(self):
        callback_id = 'button-' + str(uuid.uuid4())
        output.register_callback(callback_id, self._callback)

        template = """<div style='background-color:lightgreen'><h2>Provide your work here</h2><textarea id='d2345' style='width:500px;height:100px'></textarea>
        <br>
        <button id="{callback_id}">{title}</button>
        </div>
        <script>
          
          
          document.querySelector("#{callback_id}").onclick = (e) => {{
            var a = document.querySelector("#d2345").value;
            //console.log(a);
            google.colab.kernel.invokeFunction('{callback_id}', [a], {{}})
            e.preventDefault();
          }};
        </script>"""
        html = template.format(title=self._title, callback_id=callback_id)
        return html
Пример #18
0
def register_callback(callback_id, callback):
    def the_callback(*args, **kwargs):
        try:
            callback(*args, **kwargs)
        except Exception as err:
            traceback.print_exc()
            print('Error: ', err)

    if VDOMR_GLOBAL['mode'] == 'colab':
        from google.colab import output as colab_output  # pylint: disable=import-error
        colab_output.register_callback(callback_id, the_callback)
        exec_javascript(
            'window.vdomr_invokeFunction=google.colab.kernel.invokeFunction')
    elif ((VDOMR_GLOBAL['mode'] == 'jp_proxy_widget')
          or (VDOMR_GLOBAL['mode'] == 'server')
          or (VDOMR_GLOBAL['mode'] == 'pyqt5')):
        VDOMR_GLOBAL['invokable_functions'][callback_id] = the_callback
    ret = "(function(args, kwargs) {window.vdomr_invokeFunction('{callback_id}', args, kwargs);})"
    ret = ret.replace('{callback_id}', callback_id)
    return ret
Пример #19
0
def inspect(target):
    """Displays an interactive inspector for the given object.

  Args:
    target: the object to be inspected.
  """

    output.register_callback('inspect.create_specification_for_js',
                             create_specification_for_js)

    object_id = 'id_%s' % str(uuid.uuid4()).replace('-', '_')
    _root[object_id] = target

    display(
        IPython.display.HTML('''
    <link rel='stylesheet' href='/nbextensions/google.colab.labs.inspector/inspector.css'>
    <script src='/nbextensions/google.colab.labs.inspector/inspector.bundle.js'></script>
    <script>
      inspectSpec('{id}', {spec}, true);
    </script>
  '''.format(id=object_id, spec=_create_spec_for(target))))
Пример #20
0
def create_handler_wrapper(event_handlers):
    """Wraps the event handler and registers it as a callback for the js side.

  Wraps the event handler and registers it as a callback for js. Keep count and
  use it as aprt of the callback name to ensure uniqueness.

  Args:
    event_handlers: The hadnler for the js events.

  Returns:
    The name of the js callback.
  """
    name = 'tfma_eventCallback' + str(create_handler_wrapper.count)
    create_handler_wrapper.count += 1

    def wrapped_function(name='', detail=None):
        if event_handlers and name in event_handlers:
            event_handlers[name](detail)

    output.register_callback(name, wrapped_function)
    return name
Пример #21
0
    def __init__(self, room=None, javacript_callable=False):
        super().__init__(room)

        self._javascript_callable = javacript_callable

        if output and javacript_callable:
            output.register_callback(f'{room}.colab.signaling.connect',
                                     self.connect_sync)
            output.register_callback(f'{room}.colab.signaling.send',
                                     self.send_sync)
            output.register_callback(f'{room}.colab.signaling.receive',
                                     self.receive_sync)
            output.register_callback(f'{room}.colab.signaling.close',
                                     self.close_sync)
Пример #22
0
def watch_globals():
    user_globals = IPython.get_ipython().user_global_ns
    _root['user_global_ns'] = user_globals

    output.register_callback('inspect.create_specification_for_js',
                             create_specification_for_js)
    display(
        IPython.display.HTML('''
    <link rel='stylesheet' href='/nbextensions/google.colab.labs.inspector/inspector.css'>
    <script src='/nbextensions/google.colab.labs.inspector/inspector.bundle.js'></script>
    <script>
      inspectSpec('user_global_ns', {spec}, false);
    </script>
  '''.format(spec=json.dumps(
            _create_spec_for(
                user_globals, include_private=False, filter_global_ns=True)))))

    global _post_execute_hook
    if not _post_execute_hook:
        _post_execute_hook = _refresh_watchers
        IPython.get_ipython().events.register('post_run_cell',
                                              _post_execute_hook)
Пример #23
0
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import tensorflow as tf
from IPython import display
from google.colab import output
from witwidget.notebook import base


# Python functions for requests from javascript.
def infer_examples(wit_id):
    WitWidget.widgets[wit_id].infer()


output.register_callback('notebook.InferExamples', infer_examples)


def delete_example(wit_id, index):
    WitWidget.widgets[wit_id].delete_example(index)


output.register_callback('notebook.DeleteExample', delete_example)


def duplicate_example(wit_id, index):
    WitWidget.widgets[wit_id].duplicate_example(index)


output.register_callback('notebook.DuplicateExample', duplicate_example)
Пример #24
0
def interact(func, **kargs):

    # parse the kargs
    fixed_kargs = {}
    # others
    option_kargs = {}
    # tuple
    slider_kargs = {}
    # list, min,max,step=1.0
    slider_template = '%s :<input type="range" class="slider" id="%s" onchange="call_back()"'
    slider_template = "" + (
        slider_template +
        ' min="%3.3f" max="%3.3f" step ="%3.3f" value="%3.3f" > %3.3f <br>')

    js_update_template = '''
  <script>
      
      document.addEventListener("DOMContentLoaded",call_back(%d))
      function call_back(is_update)
      {
        if (is_update < 0)
          return ;
          
        var obj={};
        var str_func = "notebook.add_init";
        var sliders = document.getElementsByClassName("slider");
          for(var i = 0; i < sliders.length; i++)
        {
          obj[sliders[i].id] = sliders[i].value;
        }
        if (typeof is_update === "undefined")
           is_update =1    
            
        if (is_update >0 ) 
           str_func = "notebook.add_update"      
        google.colab.kernel.invokeFunction(str_func, [], obj);        
  
      } 
  
  </script> 
  '''

    for key in kargs:
        x = kargs[key]
        if type(x) is list:
            y = [x[0], x[1], 1.0, 0.5 * x[0] + 0.5 * x[1]]
            if len(x) > 2:
                y[2] = x[2]
            slider_kargs[key] = y
        elif type(x) is tuple:
            option_kargs[key] = x
        else:
            fixed_kargs[key] = x

    def generate_slider_element(a_slider_dict):
        all_html = ""
        for key in a_slider_dict:
            value = a_slider_dict[key]
            ahtml = slider_template % (key, key, value[0], value[1], value[2],
                                       value[3], value[3])
            all_html = all_html + ahtml + "\n"
        return all_html

    # do it later
    def generate_option_element(a_option_dict):  # to do later
        return ""

    def merge_dicts(*dict_args):
        """
    Given any number of dicts, shallow copy and merge into a new dict,
    precedence goes to key value pairs in latter dicts.
    """
        result = {}
        for dictionary in dict_args:
            result.update(dictionary)
        for key in result:
            if type(result[key]) is list:  # slider get value only;
                result[key] = result[key][-1]  # only get value
        return result

    def update_html(a_slider_dict, state_flag=0):

        part1_html = generate_slider_element(a_slider_dict)
        new_html = part1_html + "\n" + (js_update_template % state_flag)
        #print (new_html)
        display.display(display.HTML(new_html))
        return

    def update_plot(**js_kargs):

        display.clear_output()

        # update the current slider_kargs's value;
        for key in js_kargs:
            slider_kargs[key][3] = float(js_kargs[key])

        update_html(slider_kargs, -1)
        func_kargs = merge_dicts(fixed_kargs, slider_kargs)
        # do what the current functions does
        return func(**func_kargs)

    def init_plot(**js_kargs):

        func_kargs = merge_dicts(fixed_kargs, slider_kargs)
        return func(**func_kargs)

    update_html(slider_kargs)
    output.register_callback('notebook.add_update', update_plot)
    output.register_callback('notebook.add_init', init_plot)

    return func
Пример #25
0
def annotate(
        imgs: List[Union[str, np.ndarray]],  # pylint: disable=invalid-name
        box_storage_pointer: List[np.ndarray],
        callbackId: str = None):
    """Open the bounding box UI and prompt the user for input.

  Args:
    imgs: 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.

    box_storage_pointer: list[np.ndarray]
      Destination list for bounding box arrays. Each array in this list
      corresponds to one of the images given in imgs. The array is a
      N x 4 array where N is the number of bounding boxes given by the user
      for that particular image. If there are no bounding boxes for an image,
      None is used instead of an empty array.

    callbackId: str, optional
      The ID for the callback function that communicates between the fontend
      and the backend. If no ID is given, a random UUID string is used instead.
  """

    # Set a random ID for the callback function
    if callbackId is None:
        callbackId = str(uuid.uuid1()).replace('-', '')

    def dictToList(input_bbox):  # pylint: disable=invalid-name
        """Convert bbox.

    This function converts the dictionary from the frontend (if the format
    {x, y, w, h} as shown in callbackFunction) into a list
    ([y_min, x_min, y_max, x_max])

    Args:
      input_bbox:

    Returns:
      A list with bbox coordinates in the form [ymin, xmin, ymax, xmax].
    """
        return (input_bbox['y'], input_bbox['x'],
                input_bbox['y'] + input_bbox['h'],
                input_bbox['x'] + input_bbox['w'])

    def callbackFunction(annotations: List[List[Dict[str, float]]]):  # pylint: disable=invalid-name
        """Callback function.

    This is the call back function to capture the data from the frontend and
    convert the data into a numpy array.

    Args:
      annotations: list[list[dict[str, float]]]
        The input of the call back function is a list of list of objects
        corresponding to the annotations. The format of annotations is shown
        below

        [
          // stuff for image 1
          [
            // stuff for rect 1
            {x, y, w, h},
            // stuff for rect 2
            {x, y, w, h},
            ...
          ],
          // stuff for image 2
          [
            // stuff for rect 1
            {x, y, w, h},
            // stuff for rect 2
            {x, y, w, h},
            ...
          ],
          ...
        ]
    """

        # reset the boxes list
        nonlocal box_storage_pointer
        boxes: List[np.ndarray] = box_storage_pointer
        boxes.clear()

        # load the new annotations into the boxes list
        for annotations_per_img in annotations:
            rectangles_as_arrays = [
                np.clip(dictToList(annotation), 0, 1)
                for annotation in annotations_per_img
            ]
            if rectangles_as_arrays:
                boxes.append(np.stack(rectangles_as_arrays))
            else:
                boxes.append(None)

        # output the annotations to the errorlog
        with output.redirect_to_element('#errorlog'):
            display('--boxes array populated--')

    output.register_callback(callbackId, callbackFunction)
    draw_bbox(imgs, callbackId)
Пример #26
0
def render(box_size,
           geometry,
           buffer_size=None,
           background_color=None,
           resolution=None,
           frame_rate=None):
    """Creates a rendering front-end along with callbacks in the host program.

  Args:
    box_size: A float or an array of shape `(spatial_dimension,)`. Specifies
      the size of the simulation volume. Used to position the camera.
    geometry: A dictionary containing names paired with geometric objects such
      as Disk, Sphere, or Bond.
    buffer_size: The maximum number of timesteps to send to the font-end in a
      single call.
    background_color: An array of shape (3,) specifying the background color of
      the visualization.
    resolution: The resolution of the renderer.
    frame_rate: An optional integer specifying the target frames-per-second
      for the renderer.
  """
    global SIMULATION_IDX
    # INTERNAL_RENDERER_CODE_LOADING

    simulation_idx = SIMULATION_IDX

    frame_count = None
    dimension = None

    if not isinstance(geometry, dict):
        geometry = {'all': geometry}

    for geom in geometry.values():
        if hasattr(geom, 'position'):
            assert dimension is None or geom.position.shape[-1] == dimension
            dimension = geom.position.shape[-1]

            if geom.position.ndim == 3:
                assert frame_count is None or frame_count == geom.position.shape[
                    0]
                frame_count = geom.position.shape[0]

    assert dimension is not None

    if isinstance(box_size, jnp.ndarray):
        if box_size.shape:
            assert box_size.shape == (dimension, )
            box_size = list(box_size)
        else:
            box_size = [
                float(box_size),
            ] * dimension
    elif isinstance(box_size, float) or isinstance(box_size, int):
        box_size = [
            box_size,
        ] * dimension

    def get_metadata():
        metadata = {
            'box_size': box_size,
            'dimension': dimension,
            'geometry': [k for k in geometry.keys()],
            'simulation_idx': simulation_idx
        }

        if frame_count is not None:
            metadata['frame_count'] = frame_count

        if buffer_size is not None:
            metadata['buffer_size'] = buffer_size

        if background_color is not None:
            metadata['background_color'] = background_color

        if resolution is not None:
            metadata['resolution'] = resolution

        if frame_rate is not None:
            metadata['frame_rate'] = frame_rate

        return _to_json(metadata)

    output.register_callback('GetSimulationMetadata', get_metadata)

    def get_dynamic_geometry_metadata(name):
        assert name in geometry

        geom = geometry[name]
        geom_dict = dataclasses.asdict(geom)

        geom_metadata = {
            'shape': str(geom),
            'fields': {},
        }

        for field in geom_dict:
            if not isinstance(geom_dict[field], onp.ndarray):
                geom_metadata[field] = geom_dict[field]
                continue
            if len(geom_dict[field].shape) == TYPE_DIMENSIONS[field] + 1:
                geom_metadata['fields'][field] = 'dynamic'
            elif len(geom_dict[field].shape) == TYPE_DIMENSIONS[field]:
                geom_metadata['fields'][field] = 'static'
            elif len(geom_dict[field].shape) == TYPE_DIMENSIONS[field] - 1:
                geom_metadata['fields'][field] = 'global'
        return _to_json(geom_metadata)

    output.register_callback(f'GetGeometryMetadata{SIMULATION_IDX}',
                             get_dynamic_geometry_metadata)

    def get_array_chunk(name, field, offset, size):
        assert name in geometry

        geom = dataclasses.asdict(geometry[name])
        assert field in geom
        array = geom[field]

        return _to_json(
            {'array_chunk': _encode(array[offset:(offset + size)])})

    output.register_callback(f'GetArrayChunk{SIMULATION_IDX}', get_array_chunk)

    def get_array(name, field):
        assert name in geometry

        geom = dataclasses.asdict(geometry[name])
        assert field in geom
        array = geom[field]

        return _to_json({'array': _encode(array)})

    output.register_callback(f'GetArray{SIMULATION_IDX}', get_array)

    SIMULATION_IDX += 1

    IPython.display.display(renderer_code)
Пример #27
0
		//container.appendChild(document.createTextNode(method[i+1]+' '));
		var input = document.createElement('input');
		input.id = 'input' + i;
		input.size = '40'
		input.type = 'text';
		input.style.marginLeft= '30px';
		input.style.backgroundColor = 'white';
    input.style.color='black';
		input.placeholder = 'please enter ' + method[i] + ' here';
		container.appendChild(input);
		container.appendChild(document.createElement('br'));
	}
}
</script>
'''))
"""def add_list_item(a = 0,b=None):
  # Use redirect_to_element to direct the elements which are being written.
  with output.redirect_to_element('#items'):
    # Use display to add items which will be persisted on notebook reload.
    if a == 1: 
      display(IPython.display.HTML('<li> Another item</li>'))
    else:
      str = '<li> A %s </li>' %b
      display(IPython.display.HTML(str))
output.register_callback('notebook.AddListItem', add_list_item)"""

output_dir = "/content/to_be_fetched"


def change_download_name_display(change):
    display(
Пример #28
0
    params:
        image: image
    """
    image = data_uri_to_img(imgB64)  
    if image is None:
        print("At run_algo(): image is None.")
        return

    try:
        # Run detection
        results = model.detect([image], verbose=1)
        # Visualize results
        r = results[0]    
        visualize.display_instances(
            image, 
            r['rois'], 
            r['masks'], 
            r['class_ids'], 
            class_names, 
            r['scores']
            )
    except Exception as e:
        logging.exception(e)
        print('\n')

# register this function, so JS code could call this
output.register_callback('notebook.run_algo', run_algo)

# put the JS code in cell and run it
take_photo()
Пример #29
0
    let objDiv = document.getElementById("log");
    objDiv.appendChild(paraWithText('User: '******'Assist: ' + resp));
    objDiv.scrollTop = objDiv.scrollHeight;
};
async function colabGetResp(val) {
    let resp = await google.colab.kernel.invokeFunction(
        'notebook.get_response', [val], {});
    return resp.data['application/json']['result'];
}
async function webGetResp(val) {
    let resp = await fetch("/response.json?sentence=" + 
        encodeURIComponent(val));
    let data = await resp.json();
    return data['result'];
}
</script>
"""

import IPython
from google.colab import output

display(IPython.display.HTML(chatbot_html + \
                             "<script>let getResp = colabGetResp;</script>"))

def get_response(val):
    resp = response(val)
    return IPython.display.JSON({'result': resp})

output.register_callback('notebook.get_response', get_response)
Пример #30
0
# limitations under the License.

import json
import math
import tensorflow as tf
from IPython import display
from google.colab import output
from witwidget.notebook import base


# Python functions for requests from javascript.
def infer_examples(wit_id):
    WitWidget.widgets[wit_id].infer()


output.register_callback('notebook.InferExamples', infer_examples)


def delete_example(wit_id, index):
    WitWidget.widgets[wit_id].delete_example(index)


output.register_callback('notebook.DeleteExample', delete_example)


def duplicate_example(wit_id, index):
    WitWidget.widgets[wit_id].duplicate_example(index)


output.register_callback('notebook.DuplicateExample', duplicate_example)