Пример #1
0
class HeatmapWidget(W.DOMWidget):
    _view_name = T.Unicode('HeatmapView', sync=True)
    heatmap_data = T.List(sync=True)
    row_labels = T.List(sync=True)
    col_labels = T.List(sync=True)
    hcrow = T.List(sync=True)
    hccol = T.List(sync=True)
    minval = T.Float(sync=True)
    maxval = T.Float(sync=True)
    html_style = T.Unicode(sync=True)
Пример #2
0
class NormalizeDist(ToolBase):
    """
    A simple example Tool.

    This tool re-scales the input distribution so that the sum
    is equal to `norm_val`.


    """
    norm_val = traitlets.Float(1, tooltip='new sum', label='norm')
    input_dist = traitlets.Instance(klass=DistributionSource,
                                    tooltip='input distribution',
                                    label='input')
    output_dist = traitlets.Instance(klass=DistributionSink,
                                     tooltip='output distribution',
                                     label='output')

    def run(self):
        # sanity checks
        if self.input_dist is None:
            raise ValueError("input source must be not-none")
        if self.output_dist is None:
            raise ValueError("output sink must be not-none")

        # grab data from input
        with self.input_dist as src:
            tmp_val = np.array(src.values(), dtype='float')
            edges = src.bin_edges()

        # scale data
        tmp_val *= self.norm_val / np.sum(tmp_val)

        # write results out
        with self.output_dist as snk:
            snk.write_dist(edges, tmp_val)
Пример #3
0
    class IPythonProgressWidget(widgets.DOMWidget):
        """IPython widget for displaying a progress bar."""

        # pylint: disable=too-many-public-methods
        _view_name = traitlets.Unicode('NengoProgressBar', sync=True)
        progress = traitlets.Float(0., sync=True)
        text = traitlets.Unicode(u'', sync=True)

        FRONTEND = '''
        require(["widgets/js/widget", "widgets/js/manager"],
            function(widget, manager) {
          if (typeof widget.DOMWidgetView == 'undefined') {
            widget = IPython;
          }
          if (typeof manager.WidgetManager == 'undefined') {
            manager = IPython;
          }

          var NengoProgressBar = widget.DOMWidgetView.extend({
            render: function() {
              // $el is the DOM of the widget
              this.$el.css({width: '100%', marginBottom: '0.5em'});
              this.$el.html([
                '<div style="',
                    'width: 100%;',
                    'border: 1px solid #cfcfcf;',
                    'border-radius: 4px;',
                    'text-align: center;',
                    'position: relative;">',
                  '<div class="pb-text" style="',
                      'position: absolute;',
                      'width: 100%;">',
                    '0%',
                  '</div>',
                  '<div class="pb-bar" style="',
                      'background-color: #bdd2e6;',
                      'width: 0%;',
                      'transition: width 0.1s linear;">',
                    '&nbsp;',
                  '</div>',
                '</div>'].join(''));
            },

            update: function() {
              this.$el.css({width: '100%', marginBottom: '0.5em'});
              var progress = 100 * this.model.get('progress');
              var text = this.model.get('text');
              this.$el.find('div.pb-bar').width(progress.toString() + '%');
              this.$el.find('div.pb-text').text(text);
            },
          });

          manager.WidgetManager.register_widget_view(
            'NengoProgressBar', NengoProgressBar);
        });'''

        @classmethod
        def load_frontend(cls):
            """Loads the JavaScript front-end code required by then widget."""
            get_ipython().run_cell_magic('javascript', '', cls.FRONTEND)
Пример #4
0
class D3Sankey(widgets.DOMWidget):
    # the name of the requirejs module (no .js!)
    _view_module = traitlets.Unicode(
        'nbextensions/ipythond3sankey/js/widget_d3sankey',
        sync=True)

    # the name of the Backbone.View subclass to be used
    _view_name = traitlets.Unicode(
        'D3SankeyView',
        sync=True
    )

    # the name of the CSS file to load with this widget
    _view_style = traitlets.Unicode(
        'nbextensions/ipythond3sankey/css/widget_d3sankey',
        sync=True
    )

    # the actual value: lists of nodes and links
    nodes = traitlets.List(sync=True)
    links = traitlets.List(sync=True)

    # margins & size
    margin_top = traitlets.Float(1, sync=True)
    margin_right = traitlets.Float(1, sync=True)
    margin_bottom = traitlets.Float(6, sync=True)
    margin_left = traitlets.Float(1, sync=True)
    width = traitlets.Float(960, sync=True)
    height = traitlets.Float(500, sync=True)

    unit = traitlets.Unicode('', sync=True)
Пример #5
0
class BoundedThreshold(ToolBase):
    """
    Select a band of thresholds

    """
    input_file = traitlets.Instance(klass=ImageSource,
                                    tooltip='Image File',
                                    label='input')
    output_file = traitlets.Instance(klass=ImageSink,
                                    tooltip='Image File',
                                    label='output')
    min_val = traitlets.Float(0, tooltip='Minimum Value', label='min_val')
    max_val = traitlets.Float(1, tooltip='Maximum Value', label='max_val')

    def run(self):
        with self.input_file as src:
            # grab the input data
            res = _generic_thresh(src.get_frame(0),
                                  min_val=self.min_val,
                                  max_val=self.max_val)
        self.output_file.set_resolution(self.input_file.resolution,
                                        self.input_file.resolution_units)
        with self.output_file as snk:
            snk.record_frame(res, 0)
Пример #6
0
class IPythonProgressWidget(DOMWidget):
    """IPython widget for displaying a progress bar."""

    # pylint: disable=too-many-public-methods
    _view_name = traitlets.Unicode('NengoProgressBar', sync=True)
    if notebook_version[0] >= 4:
        _view_module = traitlets.Unicode('nengo', sync=True)
    progress = traitlets.Float(0., sync=True)
    text = traitlets.Unicode(u'', sync=True)

    WIDGET = '''
      var NengoProgressBar = widgets.DOMWidgetView.extend({
        render: function() {
          // Work-around for messed up CSS in IPython 4
          $('.widget-subarea').css({flex: '2 1 0%'});
          // $el is the DOM of the widget
          this.$el.css({width: '100%', marginBottom: '0.5em'});
          this.$el.html([
            '<div style="',
                'width: 100%;',
                'border: 1px solid #cfcfcf;',
                'border-radius: 4px;',
                'text-align: center;',
                'position: relative;">',
              '<div class="pb-text" style="',
                  'position: absolute;',
                  'width: 100%;">',
                '0%',
              '</div>',
              '<div class="pb-bar" style="',
                  'background-color: #bdd2e6;',
                  'width: 0%;',
                  'transition: width 0.1s linear;">',
                '&nbsp;',
              '</div>',
            '</div>'].join(''));
        },

        update: function() {
          this.$el.css({width: '100%', marginBottom: '0.5em'});
          var progress = 100 * this.model.get('progress');
          var text = this.model.get('text');
          this.$el.find('div.pb-bar').width(progress.toString() + '%');
          this.$el.find('div.pb-text').html(text);
        },
      });
    '''

    FRONTEND = '''
    define('nengo', ["jupyter-js-widgets"], function(widgets) {{
        {widget}

      return {{
        NengoProgressBar: NengoProgressBar
      }};
    }});'''.format(widget=WIDGET)

    LEGACY_FRONTEND = '''
    require(["widgets/js/widget", "widgets/js/manager"],
        function(widgets, manager) {{
      if (typeof widgets.DOMWidgetView == 'undefined') {{
        widgets = IPython;
      }}
      if (typeof manager.WidgetManager == 'undefined') {{
        manager = IPython;
      }}

      {widget}

      manager.WidgetManager.register_widget_view(
        'NengoProgressBar', NengoProgressBar);
    }});'''.format(widget=WIDGET)

    LEGACY_4_FRONTEND = '''
    define('nengo', ["widgets/js/widget"], function(widgets) {{
        {widget}

      return {{
        NengoProgressBar: NengoProgressBar
      }};
    }});'''.format(widget=WIDGET)

    @classmethod
    def load_frontend(cls, ipython):
        """Loads the JavaScript front-end code required by then widget."""
        if notebook_version[0] < 4:
            ipython.run_cell_magic('javascript', '', cls.LEGACY_FRONTEND)
        elif ipywidgets.version_info[0] < 5:
            nb_ver_4x = (notebook_version[0] == 4 and notebook_version[1] > 1)
            if notebook_version[0] > 4 or nb_ver_4x:
                warnings.warn(
                    "Incompatible versions of notebook and ipywidgets "
                    "detected. Please update your ipywidgets package to "
                    "version 5 or above.")
            ipython.run_cell_magic('javascript', '', cls.LEGACY_4_FRONTEND)
        else:
            ipython.run_cell_magic('javascript', '', cls.FRONTEND)
Пример #7
0
    # the content for the TextWidget must also be a traitlet
    elif class_name == "TextWidget":
        traitlets_dict.update({"content": klass.content})

    # now define the trailets with the correct traitlet.Type
    for name, default in traitlets_dict.items():

        try:
            default = float(default)
            attribute_type = "float"
        except:
            attribute_type = "string"

        if attribute_type == "float":
            setattr(klass, name, traitlets.Float(default, sync=True))
        elif attribute_type == "string":
            setattr(klass, name, traitlets.Unicode(default, sync=True))

        traitlet = getattr(klass, name)

        # normally set by metaclass IPython.utils.traitlets.MetaHasTraits.__new__
        traitlet.name = name

        # normally set by metaclass IPython.utils.traitlets.MetaHasTraits.__init__
        traitlet.this_class = klass

# prepare data to send to Javascript

widget_properties = {
    class_name: {
Пример #8
0
class TestDocument(documents.Document):
    mstr = traitlets.Unicode(default_value = "axx", db= True)
    number = traitlets.Float(db=True)
    emb = traitlets.Instance(EmbDoc, db=True)
    moreembs = traitlets.List(traitlets.Instance(EmbDoc), db=True)
    lst = documents.TList(traitlets.Int)