<commit_before>from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic)
from IPython import get_ipython
from IPython.display import publish_display_data, display, Javascript
from IPython.core.formatters import DisplayFormatter

from pprint import pprint

import argparse
import os
import sys
import shlex
import json
from time import sleep

if sys.version.startswith("3"):
    unicode = str

from twisted.internet.error import ReactorAlreadyInstalledError
from zmq.eventloop import ioloop
ioloop.install()
from tornado.ioloop import IOLoop
import tornado.platform.twisted
try:
    tornado.platform.twisted.install()
except ReactorAlreadyInstalledError:
    pass

from twisted.python import log
from twisted.internet import reactor, threads
from twisted.internet.defer import inlineCallbacks, returnValue, Deferred, CancelledError
from autobahn.twisted.wamp import ApplicationSession
from autobahn import wamp
from autobahn.wamp.exception import ApplicationError
from autobahn.websocket.util import parse_url
from autobahn.twisted.util import sleep as absleep

from juno_magic.runner import JunoRunner

from collections import deque

from sh import wampify

import requests
import re


JUNO_KERNEL_URI = os.environ.get("JUNO_KERNEL_URI", "https://juno.timbr.io/api/kernels/list")


def publish_to_display(obj):
    output, _ = DisplayFormatter().format(obj)
    publish_display_data(output)

def build_display_data(obj):
    output = {"text/plain": repr(obj)}
    methods = dir(obj)
    if "_repr_html_" in methods:
        output["text/html"] = obj._repr_html_()
    if "_repr_javascript_" in methods:
        output["application/javascript"] = obj._repr_javascript_()
    return output

def handle_comm_open(msg):
    comm_manager = get_ipython().kernel.comm_manager
    # set up the on open callback in the comm_manager for the new comms
    comm_manager.register_target(msg['content']['target_name'], on_comm_open)
    # create the Comm in the comm_manager and forward the comm and msg to on_comm_open
    comm_manager.comm_open(None, None, msg)

def on_comm_open(comm, msg):
    content = msg['content']
    comm._publish_msg('comm_open',
        data=content['data'], metadata={"echo": True}, buffers=None,
        target_name=content['target_name'],
        target_module=None
    )

def handle_comm_msg(msg):
    content = msg['content']
    comm_id = content['comm_id']
    get_ipython().kernel.comm_manager.comms[comm_id]._publish_msg('comm_msg',
        data=content['data'], metadata={"echo": True}, buffers=None
    )

def build_bridge_class(magics_instance):
    class WampConnectionComponent(ApplicationSession):
        _wamp_prefix = ""
        _ipython = get_ipython()
        _msg_id_lut = deque(maxlen=10)<commit_msg>adding a private message note<commit_after>from IPython.core.magic import (Magics, magics_class, line_magic,
                                cell_magic, line_cell_magic)
from IPython import get_ipython
from IPython.display import publish_display_data, display, Javascript
from IPython.core.formatters import DisplayFormatter

from pprint import pprint

import argparse
import os
import sys
import shlex
import json
from time import sleep

if sys.version.startswith("3"):
    unicode = str

from twisted.internet.error import ReactorAlreadyInstalledError
from zmq.eventloop import ioloop
ioloop.install()
from tornado.ioloop import IOLoop
import tornado.platform.twisted
try:
    tornado.platform.twisted.install()
except ReactorAlreadyInstalledError:
    pass

from twisted.python import log
from twisted.internet import reactor, threads
from twisted.internet.defer import inlineCallbacks, returnValue, Deferred, CancelledError
from autobahn.twisted.wamp import ApplicationSession
from autobahn import wamp
from autobahn.wamp.exception import ApplicationError
from autobahn.websocket.util import parse_url
from autobahn.twisted.util import sleep as absleep

from juno_magic.runner import JunoRunner

from collections import deque

from sh import wampify

import requests
import re


JUNO_KERNEL_URI = os.environ.get("JUNO_KERNEL_URI", "https://juno.timbr.io/api/kernels/list")


def publish_to_display(obj):
    output, _ = DisplayFormatter().format(obj)
    publish_display_data(output)

def build_display_data(obj):
    output = {"text/plain": repr(obj)}
    methods = dir(obj)
    if "_repr_html_" in methods:
        output["text/html"] = obj._repr_html_()
    if "_repr_javascript_" in methods:
        output["application/javascript"] = obj._repr_javascript_()
    return output

def handle_comm_open(msg):
    comm_manager = get_ipython().kernel.comm_manager
    # set up the on open callback in the comm_manager for the new comms
    comm_manager.register_target(msg['content']['target_name'], on_comm_open)
    # create the Comm in the comm_manager and forward the comm and msg to on_comm_open
    comm_manager.comm_open(None, None, msg)

#
# In order to relay comm_open and comm_msg message types we're calling a private method: _publish_msg
# the reason is that we effectively want to call `session.send` which _publish_msg does
# but by calling _publish_msg directly we avoid other artifacts from calling "open" and "session.send".
# (this note is meant to provide reference in case jupyter's private methods change).
#
def on_comm_open(comm, msg):
    content = msg['content']
    comm._publish_msg('comm_open',
        data=content['data'], metadata={"echo": True}, buffers=None,
        target_name=content['target_name'],
        target_module=None
    )

def handle_comm_msg(msg):
    content = msg['content']
    comm_id = content['comm_id']
    get_ipython().kernel.comm_manager.comms[comm_id]._publish_msg('comm_msg',
        data=content['data'], metadata={"echo": True}, buffers=None
    )

def build_bridge_class(magics_instance):
    class WampConnectionComponent(ApplicationSession):
        _wamp_prefix = ""
        _ipython = get_ipython()
        _msg_id_lut = deque(maxlen=10)
{'fraction_non_alphanumeric': 0.08532934131736528, 'fraction_numerical': 0.000998003992015968, 'mean_word_length': 4.598696461824954, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4013556', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1809, 'n_words': 488}