Skip to content

Commit

Permalink
Add support for jupyter widgets (#350)
Browse files Browse the repository at this point in the history
Jupyter widgets require custom metadata in open, send_msg, and close. This patch adds the ability to send metadata in a backward-compatible way.
  • Loading branch information
matt-do-it authored Oct 1, 2024
1 parent fe16c1b commit ebebd95
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions lib/iruby/comm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ def initialize(target_name, comm_id = SecureRandom.uuid)
@target_name, @comm_id = target_name, comm_id
end

def open(**data)
Kernel.instance.session.send(:publish, :comm_open, comm_id: @comm_id, data: data, target_name: @target_name)
def open(metadata = nil, **data)
Kernel.instance.session.send(:publish, :comm_open, metadata, comm_id: @comm_id, data: data, target_name: @target_name)
Comm.comm[@comm_id] = self
end

def send(**data)
Kernel.instance.session.send(:publish, :comm_msg, comm_id: @comm_id, data: data)
def send(metadata = nil, **data)
Kernel.instance.session.send(:publish, :comm_msg, metadata, comm_id: @comm_id, data: data)
end

def close(**data)
Kernel.instance.session.send(:publish, :comm_close, comm_id: @comm_id, data: data)
def close(metadata = nil, **data)
Kernel.instance.session.send(:publish, :comm_close, metadata, comm_id: @comm_id, data: data)
Comm.comm.delete(@comm_id)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/iruby/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def setup_security
end
end

def send(socket_type, message_type, content)
def send(socket_type, message_type, metadata = nil, content)
sock = check_socket_type(socket_type)
idents = if socket_type == :reply && @last_recvd_msg
@last_recvd_msg[:idents]
Expand All @@ -85,7 +85,7 @@ def send(socket_type, message_type, content)
session: @session_id,
version: '5.0'
}
@adapter.send(sock, serialize(idents, header, content))
@adapter.send(sock, serialize(idents, header, metadata, content))
end

def recv(socket_type)
Expand Down
4 changes: 2 additions & 2 deletions lib/iruby/session/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module SessionSerialize

private

def serialize(idents, header, content)
def serialize(idents, header, metadata = nil, content)
msg = [MultiJson.dump(header),
MultiJson.dump(@last_recvd_msg ? @last_recvd_msg[:header] : {}),
'{}',
MultiJson.dump(metadata || {}),
MultiJson.dump(content || {})]
frames = ([*idents].compact.map(&:to_s) << DELIM << sign(msg)) + msg
IRuby.logger.debug "Sent #{frames.inspect}"
Expand Down

0 comments on commit ebebd95

Please sign in to comment.