<commit_before>import ipywidgets as widgets
from traitlets import Unicode

@widgets.register
class Canvas(widgets.DOMWidget):
    """An example widget
    """
    _model_name = Unicode('CanvasModel').tag(sync=True)
    _model_module = Unicode('jupyter-canvas').tag(sync=True)
    _model_module_version = Unicode('^0.1.0').tag(sync=True)

    _view_name = Unicode('CanvasView').tag(sync=True)
    _view_module = Unicode('jupyter-canvas').tag(sync=True)
    _view_module_version = Unicode('^0.1.0').tag(sync=True)

    value = Unicode('Hello World!').tag(sync=True)

#------------------------------------------------

if __name__ == '__main__':
    pass<commit_msg>good progress on basic python side<commit_after>import ipywidgets
import traitlets

import numpy as np
import image_attendant as imat

from ._version import __version__


@ipywidgets.register
class Canvas(ipywidgets.DOMWidget):
    """An example widget
    """
    # Widget information
    _view_name = traitlets.Unicode('CanvasView').tag(sync=True)
    _view_module = traitlets.Unicode('jupyter-canvas').tag(sync=True)
    _view_module_version = traitlets.Unicode(__version__).tag(sync=True)

    _model_name = traitlets.Unicode('CanvasModel').tag(sync=True)
    _model_module = traitlets.Unicode('jupyter-canvas').tag(sync=True)
    _model_module_version = traitlets.Unicode(__version__).tag(sync=True)

    # Private information
    _data_compressed = traitlets.Bytes(help='Compressed image data').tag(sync=True)
    _format = traitlets.Unicode().tag(sync=True)
    # _event = traitlets.Dict().tag(sync=True)

    # Public information
    # https://developer.mozilla.org/en/docs/Web/CSS/image-rendering
    # pixelated = traitlets.Bool(False, help='Image rendering quality').tag(sync=True)

    def __init__(self, data=None, url=None, format='webp', quality=70):
        """Instantiate a new Canvas Image Widget object.

        Display images using HTML5 Canvas via Jupyter Notebook widget system.
        Image data must be a Numpy array (or equivalent) with a shape
        similar to one of the following:
            (rows, columns)    - Greyscale
            (rows, columns, 1) - Greyscale
            (rows, columns, 3) - RGB
            (rows, columns, 4) - RGBA

        If data type is neither of np.uint8 or np.int16, it will be cast to uint8 by scaling
        min(data) -> 0 and max(data) -> 255.

        If you supply a URL that points to an image then that image will be fetched and stored
        locally as a uint8 byte array.
        """
        super().__init__()

        self._data = None
        self._quality = quality
        self._format = format

        if url:
            self.url = url
        elif data is not None:
            self.data = data

    @property
    def url(self):
        return self._url

    @url.setter
    def url(self, value):
        self._url = value
        self._data_compressed, self._format = imat.download(self._url)

    @property
    def data(self):
        """Image data as Numpy array
        """
        if self._data is None and self._data_compressed is not None:
            self._data = imat.decompress(self._data_compressed)

        return self._data

    @data.setter
    def data(self, value):
        self._data = value
        self._data_compressed = imat.compress(self._data, self._format, quality=self.quality)

    @property
    def shape(self):
        return self.data.shape

#------------------------------------------------

if __name__ == '__main__':
    pass
{'fraction_non_alphanumeric': 0.13234455219623503, 'fraction_numerical': 0.005989731888191671, 'mean_word_length': 2.641744548286604, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3701169', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1037, 'n_words': 265}