Skip to content

Commit

Permalink
Add a jupyter command to define a common block
Browse files Browse the repository at this point in the history
Making it easier to share code inside jupyter notebooks
  • Loading branch information
ranlu committed Jul 2, 2024
1 parent 626eee0 commit 2fe5cae
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ipython_extensions/seuronbot_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,29 @@ def __init__(self, shell):
self.broker_url = "amqp://rabbitmq"
self.output_queue = "jupyter-output-queue"
self.input_queue = "jupyter-input-queue"
self.common_block = ""
self.slack_conv = markdown.Markdown(extensions=[slack_md.SlackExtension()])
drain_messages(self.broker_url, self.input_queue, verbose=False)
drain_messages(self.broker_url, self.output_queue, verbose=False)
threading.Thread(target=self.forward_bot_message).start()

@line_cell_magic
def seuronbot(self, command, cell=None):
if command == "define common block":
if cell:
self.common_block = self.shell.input_transformer_manager.transform_cell(cell)
exec(self.common_block, globals())
else:
self.common_block = ""
print("Emptying seuronbot common block")
return

msg_payload = {
"text": command,
"from_jupyter": True,
}
if cell:
code_content = self.shell.input_transformer_manager.transform_cell(cell)
code_content = self.common_block + "\n" + self.shell.input_transformer_manager.transform_cell(cell)
msg_payload["attachment"] = base64.b64encode(code_content.encode("utf-8")).decode("utf-8")

put_message(self.broker_url, self.input_queue, msg_payload)
Expand Down

0 comments on commit 2fe5cae

Please sign in to comment.