# How to copy some variables from one notebook to another using different kernel?

For example,in windows machine there is a variable testData

resource = ResourceObject["MNIST"];
testData = ResourceData[resource, "TestData"];


And I want to copy this variable to my linux machine.

There are two notebooks(both using Windows platform),one use local kernel(windows) and another use remote kernel(linux).

I try to transfer it to ByteArray then copy and paste.

Have a good idea?

• You should look for the DistributeDefinitions or SetSharedVariable and related. These functions share the data from master-kernel to sub-kernels. – Rom38 Sep 20 '17 at 5:55
• This is exactly the situation where you might want to use a MathLink between your two sessions. – halirutan Sep 20 '17 at 5:58
• @Rom38 they are all about parallel.I only want to move data – partida Sep 20 '17 at 8:47
• Perhaps you can use the Network Programming guide to create a socket connection between the two computers and transfer the data that way. – Edmund Sep 20 '17 at 9:30
• @Edmund I think you misunderstood what I mean. I'm just opening two notebooks in one computer now.I just want to copy variable from one notebook to another,don't need socket – partida Sep 20 '17 at 9:33

As a simple solution, you could use CloudPut, CloudGet and CloudObject.

list = RandomReal[1,10];
CloudPut[list, "myRandomList"]


Then in the other kernel:

CloudGet["myRandomList"]


You need to be signed in to the same Wolfram account from both locations.

As a more direct method, you could establish a MathLink (WSTP) connection between the two kernels. This method is much more fickle, and not very tolerant to user mistakes. It also requires that the two computers be able to see each other over the network and be allowed to communicate over the ports mentioned in the link name.

Here's a short example:

In kernel 1,

link = LinkCreate[LinkProtocol -> "TCPIP"]


Now First[link] will give you the link name as a string. You will need this for the other kernel.

The following call will block until the connection has been established successfully.

LinkActivate[link]


Now in kernel 2,

link = LinkConnect[linkName, LinkProtocol -> "TCPIP"]


linkName is what you got from kernel 1. It consists of port1@ip,port2@ip. Make sure that the IP address is written in a format that can be used on this second computer to reach the first (i.e. not 127.0.0.1).

LinkActivate[link]


At this point, the LinkActivate call should return on kernel 1 as well.

Now you can send data. On one kernel, LinkWrite[link, data], then on the other kernel, LinkRead[link].

Reference:

• Easier than the CloudPut setup might be to create a ChannelObject. I've passed data from a cloud session to local session like this as a proof of concept. – b3m2a1 Sep 20 '17 at 15:44
• It's a possibility ... but I think we have differing concepts of "easy". – Szabolcs Sep 20 '17 at 15:46
• There also are CloudSymbol["myVar"] and PersistentValue["myVar", "Cloud"] which seem to be designed specifically for this task. – Alexey Popkov Sep 22 '17 at 1:40

You could encode data into an Image:

(* NOTEBOOK 1 *)

encode = Image[{ToCharacterCode @ Compress @ #}, ImageSize -> {100, 100}] &;

testData = ExampleData[{"Text", "DeclarationOfIndependence"}];

encode[testData]   (* copy apparently blank output image *)

(* NOTEBOOK 2 *)

decode = Uncompress @ FromCharacterCode @ First @ Round @ ImageData @ # &;

(* paste image *) // decode


When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume, among the Powers ...

• If copying from one notebook and pasting to another is an option, I like the SaveToCell function here: szhorvat.net/pelican/save-data-in-notebooks.html – Jason B. Sep 20 '17 at 21:11
• @JasonB. interesting article – partida Sep 21 '17 at 1:35