def test_load_connection_info(): client = KernelClient() info = { 'control_port': 53702, 'hb_port': 53705, 'iopub_port': 53703, 'ip': '0.0.0.0', 'key': 'secret', 'shell_port': 53700, 'signature_scheme': 'hmac-sha256', 'stdin_port': 53701, 'transport': 'tcp', } client.load_connection_info(info) assert client.control_port == info['control_port'] assert client.session.key.decode('ascii') == info['key'] assert client.ip == info['ip']
def test_load_connection_info(): client = KernelClient() info = { "control_port": 53702, "hb_port": 53705, "iopub_port": 53703, "ip": "0.0.0.0", "key": "secret", "shell_port": 53700, "signature_scheme": "hmac-sha256", "stdin_port": 53701, "transport": "tcp", } client.load_connection_info(info) assert client.control_port == info["control_port"] assert client.session.key.decode("ascii") == info["key"] assert client.ip == info["ip"]
def run(*, connection_name: str) -> None: client = KernelClient() client.load_connection_file(connection_name) bc = client.blocking_client() codes = [ """ g = globals() if "x" not in g: x = 1 else: x += 1 """, """print(f"hello: {x}")""", """x * x""", ] for code in codes: code = textwrap.dedent(code) data = request(bc, code, timeout=1) if data is not None: print(data)
def execute_code(code: str, client: KernelClient): msg_id = client.execute(code, user_expressions={"str": "bytes"}) state = "busy" data = {} while state != "idle" and client.is_alive(): try: msg = client.get_iopub_msg(msg_id, timeout=1) if not "content" in msg: continue content = msg["content"] if msg["msg_type"] == "error": logger.opt(exception=True).error("\n".join( content["traceback"])) if "data" in content: data = content["data"] elif "text" in content: data = content["text"] if "execution_state" in content: state = content["execution_state"] except Empty: pass if "text/plain" in data: data = data["text/plain"] return data
def main(kid, var, pid): # load connection info and init communication cf = find_connection_file(kid) # str(port)) km = KernelClient(connection_file=cf) km.load_connection_file() # km.start_channels() # Step 0: get all the inputs load_input_code = f""" proc_id="{pid}" var={var} var_name="{var}" sql_name = "{cfg.sql_name}" sql_password = "******" sql_dbname = "{cfg.sql_dbname}" sql_schema_name = "{cfg.sql_schema_name}" sql_table_name = "{cfg.sql_table_name}" json_file_name = "/Users/peterchan/Desktop/GitHub/jupyter-extension/juneau_extension/data_file.json" """ # Step 1: access the table and convert it to JSON request_var_code = f""" import numpy as np import pandas as pd import json if type(var) is pd.DataFrame or type(var) is np.ndarray or type(var) is list: df_json_string = var.to_json(orient='split', index=False) df_json = json.loads(df_json_string)['data'] """ # Step 2: define the functions used to write to the JSON file json_lock_code = """ def initialize(): data = { "ownerID": "", "id123": "operating", "id124": "finish" } with open("./juneau_extension/data_file.json", "w") as file: json.dump(data, file, indent=4) def acquire_lock(pid): with open(json_file_name, "r+") as file: try: data = json.load(file) if data["ownerID"]: return False else: file.seek(0) file.truncate() data['ownerID'] = pid json.dump(data, file, indent=4) return True except Exception: return False def release_lock(pid): with open(json_file_name, "r+") as file: data = json.load(file) if data['ownerID'] == pid: file.seek(0) file.truncate() data['ownerID'] = "" json.dump(data, file, indent=4) # input: id of the process # remove from the file if the process is completed/ terminated/ timed out def update_exec_status(status, pid): done = False while not done: success = acquire_lock(pid) if success: try: with open(json_file_name, "r+") as file: data = json.load(file) if not data['ownerID'] == pid: continue file.seek(0) file.truncate() data[pid] = status json.dump(data, file, indent=4) release_lock(pid) done = True except Exception: continue return True """ # Step 3: connect to SQL and insert the table insert_code = """ from sqlalchemy import create_engine conn_string = f"postgresql://{sql_name}:{sql_password}@localhost/{sql_dbname}" table_string = f"{sql_schema_name}.{sql_table_name}" engine = create_engine(conn_string) with engine.connect() as connection: insertion_string = f'CREATE TABLE {sql_schema_name}.{var_name} ("A" int, "B" int, "C" int, "D" int);' for ls in df_json: insertion_string += f"INSERT INTO {sql_schema_name}.{var_name} VALUES ({ls[0]}, {ls[1]}, {ls[2]}, {ls[3]});" connection.execute(insertion_string) update_exec_status("done", proc_id) """ insert_fake_code = """ insertion_string = f'CREATE TABLE {sql_schema_name}.{var_name} ("A" int, "B" int, "C" int, "D" int);' for ls in df_json: insertion_string += f"INSERT INTO {sql_schema_name}.{var_name} VALUES ({ls[0]}, {ls[1]}, {ls[2]}, {ls[3]});" update_exec_status("done", proc_id) """ code = load_input_code + request_var_code + json_lock_code + insert_code # code = load_input_code + request_var_code + json_lock_code + insert_fake_code km.execute(code, store_history=False)
def setUp(self): client = KernelClient() self.shell = ZMQTerminalInteractiveShell(kernel_client=client) self.raw = b'dummy data' self.mime = 'image/png' self.data = {self.mime: base64.encodestring(self.raw).decode('ascii')}
from jupyter_client import KernelClient # jupyter_client.connect.ConnectionFileMixin.write_connection_fileでfileが出力される fpath = "/run/user/1000/jupyter/kernel-91949ec0-0f35-4f29-98e9-8c8b4931175c.json" client = KernelClient() client.load_connection_file(fpath) # print(client.get_connection_info()) bc = client.blocking_client() codes = [ """x=1""", """print(f"hello: {x}")""", ] for code in codes: # https://jupyter-client.readthedocs.io/en/stable/messaging.html?highlight=idle#kernel-status # state = busy, idle, starting state = "busy" msg_id = bc.execute(code) while state != "idle" and bc.is_alive(): msg = bc.get_iopub_msg(timeout=1) content = msg.get('content') if content is None: continue if 'execution_state' in content: state = content['execution_state'] print(f"{state}> content: {content}")