Exemplo n.º 1
0
def test_json():
    d = {'a': 5}
    lis = [d]
    metadata = [
        {
            'expanded': False,
            'root': 'root'
        },
        {
            'expanded': True,
            'root': 'root'
        },
        {
            'expanded': False,
            'root': 'custom'
        },
        {
            'expanded': True,
            'root': 'custom'
        },
    ]
    json_objs = [
        display.JSON(d),
        display.JSON(d, expanded=True),
        display.JSON(d, root='custom'),
        display.JSON(d, expanded=True, root='custom'),
    ]
    for j, md in zip(json_objs, metadata):
        nt.assert_equal(j._repr_json_(), (d, md))

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        j = display.JSON(json.dumps(d))
        nt.assert_equal(len(w), 1)
        nt.assert_equal(j._repr_json_(), (d, metadata[0]))

    json_objs = [
        display.JSON(lis),
        display.JSON(lis, expanded=True),
        display.JSON(lis, root='custom'),
        display.JSON(lis, expanded=True, root='custom'),
    ]
    for j, md in zip(json_objs, metadata):
        nt.assert_equal(j._repr_json_(), (lis, md))

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        j = display.JSON(json.dumps(lis))
        nt.assert_equal(len(w), 1)
        nt.assert_equal(j._repr_json_(), (lis, metadata[0]))
Exemplo n.º 2
0
    def show(self, format=None):
        """show the data object content in Jupyter

        :param format: format to use (when there is no/wrong suffix), e.g. 'png'
        """
        if not is_ipython:
            logger.warning(
                "Jupyter/IPython was not detected, .show() will only display inside Jupyter"
            )
            return

        from IPython import display

        suffix = self.suffix.lower()
        if format:
            suffix = "." + format

        if suffix in [".jpg", ".png", ".gif"]:
            display.display(display.Image(self.get(), format=suffix[1:]))
        elif suffix in [".htm", ".html"]:
            display.display(display.HTML(self.get(encoding="utf-8")))
        elif suffix in [".csv", ".pq", ".parquet"]:
            display.display(self.as_df())
        elif suffix in [".yaml", ".txt", ".py"]:
            display.display(display.Pretty(self.get(encoding="utf-8")))
        elif suffix == ".json":
            display.display(display.JSON(orjson.loads(self.get())))
        elif suffix == ".md":
            display.display(display.Markdown(self.get(encoding="utf-8")))
        else:
            logger.error(f"unsupported show() format {suffix} for {self.url}")
Exemplo n.º 3
0
def create_json_cell(json_dict):
    _load_ipython()
    Idp.display(Idp.JSON(json_dict))
Exemplo n.º 4
0
 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
Exemplo n.º 5
0
 def _recv(msg):
     return display.JSON(
         callback(msg))  # Use display.JSON to transfer an object
Exemplo n.º 6
0
 def draw_synthetic_input_callback(x):
     x = json.loads(x)  # parse json from string
     callback(x)
     return ipd.JSON({'result': "parsed synthetic input"})