Beispiel #1
0
 def load_json(self, node):
     data = node["data"]
     data.pop("_label", None)
     data.pop("_attrs", None)
     with self.json_output:
         self.json_output.clear_output()
         display(JSON(data))
Beispiel #2
0
 def widget(self):
     # This is ugly right now; need to get the labels all the same size
     stats = ipywidgets.VBox([
         ipywidgets.HBox([
             ipywidgets.Label("Domain Left Edge",
                              layout=ipywidgets.Layout(width="20%")),
             display_ytarray(self.ds.domain_left_edge),
         ]),
         ipywidgets.HBox([
             ipywidgets.Label("Domain Right Edge",
                              layout=ipywidgets.Layout(width="20%")),
             display_ytarray(self.ds.domain_right_edge),
         ]),
         ipywidgets.HBox([
             ipywidgets.Label("Domain Width",
                              layout=ipywidgets.Layout(width="20%")),
             display_ytarray(self.ds.domain_width),
         ]),
     ])
     # We round-trip through a JSON encoder to recursively convert stuff to lists
     dumped = json.dumps(self.ds.parameters,
                         cls=NumpyEncoder,
                         sort_keys=True)
     loaded = json.loads(dumped)
     out = ipywidgets.Output()
     with out:
         display(JSON(loaded, root="Parameters", expanded=False))
     return ipywidgets.VBox([stats, out])
Beispiel #3
0
def notebook_json(json_object):
    """Display IPython JSON object in the notebook

    :param json_object: item to show
    """
    if json_object:
        display(JSON(json_object))
def figcaption(text, label=" "):
    display(HTML("<div class=caption><b> Caption: </b> %s</div>"
                 % text.replace('\n', '<br>')))
    text = text.replace('<b>',r'\textbf{').replace('</b>','}') #some replacement of HTML formatting
    text = text.replace('<i>',r'\textit{').replace('</i>','}')
    text = text.replace('<tt>',r'\texttt{').replace('</tt>','}')
    display(JSON({'caption': text, 'label':label}),include=["application/json"])
Beispiel #5
0
    def load_json(self, change):
        if change.new == change.old:
            return None

        # must be copy to prevent changing the object
        data = dict(change.new.data)
        data.pop("_label", None)  # TODO just remove private and non-serializable
        with self.json_output:
            self.json_output.clear_output()
            display(JSON(data))
Beispiel #6
0
 def to_styled_class(item, **kwargs):
     if kwargs.get("json_display") != "raw" and (isinstance(item, dict)
                                                 or isinstance(item, list)):
         if kwargs.get("json_display") == "formatted" or kwargs.get(
                 "notebook_app") != "jupyterlab":
             return _getitem_FormattedJson(item)
         else:
             return JSON(item)
     else:
         return item
Beispiel #7
0
    def display(self, line, cell=''):
        """
        Display something using IPython's rich display system.

        Parameters
        ----------
        -h, --html    : load HTML file
        -i, --image   : load JPG or PNG image
        -j, --json    : load JSON file
        -l, --latex   : load LaTeX file
        -m, --math    : load LaTeX math expression
        -s, --svg     : load SVG file
        -y, --youtube : load YouTube ID

        Examples
        --------
        %display -i myimage.png

        %display -m '\Delta x + y^3'

        %%display -h \"\"\"
        <ul>
        <li>This</li>
        <li>is</li>
        <li>a list.</li>
        </ul>
        \"\"\"

        Notes
        -----
        %disp is automatically aliased to %display.
        """
        
        opts, cell = self.parse_options('%s\n%s' % (line, cell),
                                        'h:i:j:l:m:s:y:',
                                        ['image=', 'html=', 'json=', 'latex=',
                                         'math=', 'svg=', 'youtube='])
        for opt, arg in opts.iteritems():
            if opt in ['h', 'html']:
                return HTML(arg)
            elif opt in ['i', 'image']:
                return Image(arg)
            elif opt in ['j', 'json']:
                return JSON(arg)
            elif opt in ['l', 'latex']:
                return Latex(arg)
            elif opt in ['m', 'math']:
                return Math(arg)
            elif opt in ['s', u's', 'svg']:
                return SVG(arg)
            elif opt in ['y', 'youtube']:
                return YouTubeVideo(arg)

        # Raise an exception if no options were specified:
        raise ValueError('Format: [option] <file|URI>')
Beispiel #8
0
    def fhir_validate(self, line):
        """Line magic to validate json FHIR resource(s) from iPython kernel."""
        args = magic_arguments.parse_argstring(self.fhir_validate, line)

        with grpc.insecure_channel(self.grpc_target) as channel:
            stub = wstlservice_pb2_grpc.WhistleServiceStub(channel)

            (resp, err) = _get_validation(stub, self.shell, args.version,
                                          args.input)
            if err:
                return err
            return JSON(str(json_format.MessageToDict(resp)))
Beispiel #9
0
 def wstl_reset(self, line):
     """Cell magic to clear all variables and functions from incremental transformation."""
     with grpc.insecure_channel(self.grpc_target) as channel:
         stub = wstlservice_pb2_grpc.WhistleServiceStub(channel)
         session_id = str(self.shell.history_manager.session_number)
         req = wstlservice_pb2.DeleteIncrementalSessionRequest(
             session_id=session_id)
         try:
             resp = stub.DeleteIncrementalSessionRequest(req)
         except grpc.RpcError as rpc_error:
             return rpc_error
         else:
             return JSON(json_format.MessageToDict(resp))
Beispiel #10
0
    def load_hl7v2_datastore(self, line):
        """Load parsed HL7v2 massage from the HL7v2 Store specified."""
        args = magic_arguments.parse_argstring(self.load_hl7v2_datastore, line)

        hl7v2_messages = _get_message_from_hl7v2_store(
            args.api_version, args.project_id, args.region, args.dataset_id,
            args.hl7v2_store_id, args.filter)
        if args.dest_file_name:
            with open(args.dest_file_name, "w") as dest_file:
                dest_file.write(hl7v2_messages)
                return "The message was written to {} successfully.".format(
                    args.dest_file_name)

        return JSON(hl7v2_messages)
Beispiel #11
0
def view_tree(file: types_path_and_file_like, path: Tuple = None, **kwargs):
    """Display YAML header using IPython JSON display repr.

    This function works in JupyterLab.

    Parameters
    ----------
    file :
        filename or file-like object pointing towards / containing an ASDF file.
    path :
        tuple representing the lookup path in the yaml/asdf tree
    kwargs
        kwargs passed down to JSON constructor

    Returns
    -------
    IPython.display.JSON
        JSON object for rich output in JupyterLab

    Examples
    --------
    Visualize the full tree of an existing ASDF file::

        weldx.asdf.utils.view_tree("single_pass_weld_example.asdf")

    Visualize a specific element in the tree structure by proving the path::

        weldx.asdf.utils.view_tree(
            "single_pass_weld_example.asdf", path=("process",)
        )

        weldx.asdf.utils.view_tree(
            "single_pass_weld_example.asdf", path=("process", "welding_process")
        )

    """
    from IPython.display import JSON

    if isinstance(file, str):
        root = file + "/"
    else:
        root = "/"

    yaml_dict = get_yaml_header(file, parse=True)
    if path:
        root = root + "/".join(path)
        yaml_dict = get_path(yaml_dict, path)
    kwargs["root"] = root
    return JSON(yaml_dict, **kwargs)
Beispiel #12
0
def pprint_json(data):
    """
    Display a tree-like object in a jupyter notebook.
    Allows for collapsable interactive interaction with data.

    Args:
        data: a dictionary or object

    Based on:
    https://gist.github.com/jmmshn/d37d5a1be80a6da11f901675f195ca22

    """
    from IPython.display import JSON, display

    display(JSON(loads(DisplayEcoder().encode(data))))
Beispiel #13
0
def asdf_json_repr(file, path: Tuple = None, **kwargs):
    """Display YAML header using IPython JSON display repr.

    This function works in JupyterLab.

    Parameters
    ----------
    file
        filename or BytesIO buffer of ASDF file
    path
        tuple representing the lookup path in the yaml/asdf tree
    kwargs
        kwargs passed down to JSON constructor

    Returns
    -------
    IPython.display.JSON
        JSON object for rich output in JupyterLab

    Examples
    --------
    Visualize the full tree of an existing ASDF file::

        weldx.asdf.utils.asdf_json_repr("single_pass_weld_example.asdf")

    Visualize a specific element in the tree structure by proving the path::

        weldx.asdf.utils.asdf_json_repr(
            "single_pass_weld_example.asdf", path=("process", "welding_process")
        )


    """
    from IPython.display import JSON

    if isinstance(file, str):
        root = file + "/"
    else:
        root = "/"

    code = get_yaml_header(file)
    yaml_dict = yaml.load(code, Loader=yaml.BaseLoader)
    if path:
        root = root + "/".join(path)
        yaml_dict = get_path(yaml_dict, path)
    kwargs["root"] = root
    return JSON(yaml_dict, **kwargs)
Beispiel #14
0
    def asdf_json_repr(file, **kwargs):
        """Display YAML header using IPython JSON display repr.

        This function works in JupyterLab.

        Parameters
        ----------
        file
            filename or BytesIO buffer of ASDF file
        kwargs
            kwargs passed down to JSON constructor

        Returns
        -------
        IPython.display.JSON
            JSON object for rich output in JupyterLab

        """
        code = _get_yaml_header(file)
        yaml_dict = yaml.load(code, Loader=yaml.BaseLoader)
        return JSON(yaml_dict, **kwargs)
Beispiel #15
0
    def load_hl7v2_gcs(self, line):
        """Load and return parsed HL7v2 massage from the blob in a GCS bucket specified."""
        args = magic_arguments.parse_argstring(self.load_hl7v2_gcs, line)
        dest_file_name = args.dest_file_name

        storage_client = storage.Client()

        bucket = storage_client.bucket(args.bucket_name)
        if not bucket.exists():
            raise ValueError(
                "The bucket does not exist. Please check the provided bucket name."
            )
        blob = bucket.get_blob(args.source_blob_name)
        if not blob:
            raise ValueError(
                "The blob does not exist. Please check the provided blob name."
            )
        content = blob.download_as_string()

        if blob and blob.content_encoding:
            content = content.decode(blob.content_encoding)

        # check if the returned content is a json
        try:
            try:
                result = json.loads(content)
            except TypeError:
                result = json.loads(content.decode("UTF-8"))
        except json.JSONDecodeError:
            print(
                "The loaded content is not a valid JSON. Please check the source bucket and blob."
            )
            raise
        if dest_file_name:
            with open(dest_file_name, "w") as dest:
                dest.write(content)
                return "The message was written to {} successfully.".format(
                    dest_file_name)

        return JSON(result)
def display_json(doc: Dict[str, Any],
                 root: Optional[str] = "root") -> IPython.core.display.JSON:
    """
    Create a JSON display object given raw JSON data.

    Parameters
    ----------
    doc : JSON dict or list
        Raw JSON data to display.
    root : str, optional
        The name of the root element of the JSON tree (default 'root').

    Returns
    -------
    IPython display of JSON data : IPython.core.display.JSON
        An IPython JSON display representing the data.

    Example
    -------
    >>> from aws.utils.notebooks import json  as json_utils
    >>> json_utils.display_json(doc=my_schemas ,root="database")
    """

    return JSON(doc)
Beispiel #17
0
    def wstl(self, line, cell):
        """Cell magic to evaluate whistle mapping language from iPython kernel."""
        args = magic_arguments.parse_argstring(self.wstl, line)

        # TODO (b/157468786): migrate to secure channel.
        with grpc.insecure_channel(self.grpc_target) as channel:
            stub = wstlservice_pb2_grpc.WhistleServiceStub(channel)

            (incremental_session,
             err) = _get_or_create_session(stub, self.shell)
            if err:
                return err

            (transform,
             err) = _get_incremental_transform(stub, self.shell,
                                               incremental_session.session_id,
                                               args, cell)
            if err:
                return err

            result = _response_to_json(transform)
            if args.output:
                self.shell.push({args.output: result})
            return JSON(result)
Beispiel #18
0
# Render dicts as json in jupyter notebooks
if get_ipython().__class__.__name__ == 'ZMQInteractiveShell':
    from IPython.display import JSON
    from json import JSONEncoder, loads

    class MyEncoder(JSONEncoder):
        def default(self, o):
            try:
                return o.as_dict()
            except:
                try:
                    return o.__dict__
                except:
                    return str(o)

    show_json = lambda x: display(JSON(loads(MyEncoder().encode(x))))
Beispiel #19
0
 def _ipython_display_(self):
     fields_dict = {
         field.name: str(getattr(self, field.name))
         for field in fields(self)
     }
     display(JSON(fields_dict))
Beispiel #20
0
import numpy as np
import matplotlib.pyplot as plt
from plotly import offline as py

py.init_notebook_mode()

t = np.linspace(0, 20, 500)
plt.plot(t, np.sin(t))

py.iplot_mpl(plt.gcf())

# %%
from IPython.display import JSON

data = {"foo": {"bar": "baz"}, "a": 1}
JSON(data)

# %%
import sympy as sp

sp.init_printing(use_latex="mathjax")

x, y, z = sp.symbols("x y z")
f = sp.sin(x * y) + sp.cos(y * z)
sp.integrate(f, x)

# %%
from IPython.display import Math

Math(
    r"i\hbar \frac{dA}{dt}~=~[A(t),H(t)]+i\hbar \frac{\partial A}{\partial t}."
                        entry_value = "Bytes: {}".format(str(vbytes))
                        if vbytes < 100000:
                            pass
                        elif vbytes < Mb:
                            entry_value += " ({} kb)".format(vbytes / kb)
                        else:
                            entry_value += " ({} Mb)".format(vbytes / Mb)
                    except:
                        pass
                elif isscalar(val):
                    entry_value = str(val)

                manifest.append({
                    'name': name,
                    'size': entry_size,
                    'type': entry_type,
                    'value': entry_value,
                })
            self.report({'variables': manifest})

    get_ipython()._hydrogen_python = HydrogenPythonPlugin()

    return {'success': True}


try:
    result = install_kernel_plugin()
    display(JSON({'hydrogen_python': result}))
except:
    display(JSON({'hydrogen_python': {'success': False}}))
Beispiel #22
0
def dict2json(d):
    return JSON(d)
Beispiel #23
0
    def _ipython_display_(self):
        from IPython.display import display, JSON

        display(JSON(to_dict_basic_types(self._map)))
Beispiel #24
0
    def _ipython_display_(self, **kwargs):
        from IPython.display import JSON, display

        display(JSON(self.dict()))
Beispiel #25
0
from IPython.display import display, JSON
display(JSON({'hydrogen_python': "pass"}))
  def test_fhir_validate_magic_ipython(self, mock_stub, mock_channel):

    class FakeChannel:

      def __init__(self, channel):
        self.channel = channel

      def __enter__(self):
        return self.channel

      def __exit__(self, exc_type, exc_val, exc_tb):
        self.channel._close()
        return False

    class FakeService:

      def __init__(self, res):
        self.resp = res

      def FhirValidate(self, req):
        del req
        return self.resp

    mock_channel.return_value = FakeChannel(self._channel)
    ip = self.shell.get_ipython()
    failure = ip.magics_manager.register(wstl.WSTLMagics)
    self.assertIsNone(failure)
    st1 = "{'id':'example','resourceType':'Device','udi':{'carrierHRF':'test'}}"
    st2 = "{'id':'example','resourceType':'3','udi':{'carrierHRF':'test'}}"
    stList = [st1, st1]
    ip.push("st1")
    ip.push("st2")
    ip.push("stList")
    lines = [
        "--version=stu3 --input=py://st1", "--version=stu3 --input=py://st2",
        "--version=stu3 --input=py://stList",
        "--version=stu3 --input=pylist://stList"
    ]
    results = []
    resps = [
        wstlservice_pb2.ValidationResponse(status=[
            status_pb2.Status(code=code_pb2.OK, message="Validation Success")
        ]),
        wstlservice_pb2.ValidationResponse(status=[
            status_pb2.Status(
                code=code_pb2.INVALID_ARGUMENT, message="invalid FHIR resource")
        ]),
        wstlservice_pb2.ValidationResponse(status=[
            status_pb2.Status(
                code=code_pb2.INVALID_ARGUMENT, message="invalid FHIR resource")
        ]),
        wstlservice_pb2.ValidationResponse(status=[
            status_pb2.Status(code=code_pb2.OK, message="Validation Success")
        ]),
    ]
    reqs = [
        wstlservice_pb2.ValidationRequest(
            fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3,
            input=[
                wstlservice_pb2.Location(
                    inline_json="{'id':'example','resourceType':" +
                    "'Device','udi':{'carrierHRF':'test'}}")
            ]),
        wstlservice_pb2.ValidationRequest(
            fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3,
            input=[
                wstlservice_pb2.Location(
                    inline_json="{'id':'example','resourceType':" +
                    "'3','udi':{'carrierHRF':'test'}}")
            ]),
        wstlservice_pb2.ValidationRequest(
            fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3,
            input=[
                wstlservice_pb2.Location(
                    inline_json="[\"{'id':'example','resourceType':" +
                    "'Device','udi':{'carrierHRF':'test'}}\", " +
                    "\"{'id':'example','resourceType':" +
                    "'Device','udi':{'carrierHRF':'test'}}\"]")
            ]),
        wstlservice_pb2.ValidationRequest(
            fhir_version=wstlservice_pb2.ValidationRequest.FhirVersion.STU3,
            input=[
                wstlservice_pb2.Location(
                    inline_json="{'id':'example','resourceType':" +
                    "'Device','udi':{'carrierHRF':'test'}}"),
                wstlservice_pb2.Location(
                    inline_json="{'id':'example','resourceType':" +
                    "'Device','udi':{'carrierHRF':'test'}}"),
            ]),
    ]
    for i in range(len(lines)):
      mock_service = mock.create_autospec(FakeService)
      mock_service.FhirValidate.return_value = resps[i]
      mock_stub.return_value = mock_service
      result = ip.run_line_magic("fhir_validate", lines[i])
      results.append(result)
      mock_service.FhirValidate.assert_called_once_with(reqs[i])

    wants = [
        "{'status': [{'message': 'Validation Success'}]}",
        "{'status': [{'code': 3, 'message': 'invalid FHIR resource'}]}",
        "{'status': [{'code': 3, 'message': 'invalid FHIR resource'}]}",
        "{'status': [{'message': 'Validation Success'}]}",
    ]
    for j in range(len(wants)):
      result = results[j]
      want = JSON(wants[j])
      self.assertEqual(
          result.data,
          want.data,
          msg="JSON.data mismatch on input {}".format(lines[j]))
      self.assertEqual(
          result.url,
          want.url,
          msg="JSON.url mismatch on input {}".format(lines[j]))
      self.assertEqual(
          result.filename,
          want.filename,
          msg="JSON.filename mismatch on input {}".format(lines[j]))
    # Delete created variables to suppress the unused-variable linter warning.
    del st1
    del st2
    del stList
Beispiel #27
0
def as_json(obj):
    return JSON(json.dumps(obj.__dict__), expanded=True)
 def report(self, val):
     display(JSON({'hydrogen_python': val}))
Beispiel #29
0
def display_dict2json(d):
    display(JSON(d))
Beispiel #30
0
    }))

    return df_summary


reverse_dict = lambda original_dict: {
    value: key
    for key, value in original_dict.items()
}

capfirst = lambda s: s[:1].upper() + s[1:]

keep_index = lambda df, func, *args, **kwargs: pd.DataFrame(
    func(df, *args, **kwargs), index=df.index)

view_dict_head = lambda d, n=5: JSON(pd.Series(d).head(n).to_dict())

# Slack
msg_2_data = lambda record_msg: f'{"{"}"text":"{record_msg}"{"}"}'


def send_slack_msg(
    record_msg,
    slack_channel_url="",
):
    headers = {"Content-type": "application/json"}
    data = msg_2_data(record_msg)

    try:
        requests.post(slack_channel_url, headers=headers, data=data)
    except: