Skip to content

Instantly share code, notes, and snippets.

@ajkerrigan
Last active June 9, 2023 05:18
Show Gist options
  • Save ajkerrigan/74e4c63aec6b76f0dc5ae2edeac82b55 to your computer and use it in GitHub Desktop.
Save ajkerrigan/74e4c63aec6b76f0dc5ae2edeac82b55 to your computer and use it in GitHub Desktop.
Passing Data with VisiData Remote Control

Passing Data with VisiData Remote Control

This is a neat discussion thread where a core goal is to send data to an existing VisiData session.

This sample explores one way to do that, combining:

Demo at:

asciicast

import socket
from pathlib import Path
import pandas as pd
import ray
# Start Ray and take note of its starting address
ray.init()
ray_address = ray.runtime_context.get_runtime_context().gcs_address
# Store a Pandas DataFrame in Ray's in-memory object store,
# and record a reference that we can unwrap later
df = pd.read_csv("~/code/visidata/sample_data/benchmark.csv")
print("Loaded DataFrame with head:")
print(df.head())
serialized_ref = ray.cloudpickle.dumps(ray.put(df))
# Connect to a VisiData remote control socket. Assumes
# VisiData has been started with:
#
# vd server://moo
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
print("Opening DataFrame in running VisiData instance")
s.connect(str(Path("~/.visidata/run/moo").expanduser()))
# Send a remote control command that:
# - Connects to a local Ray instance
# - Fetches a DataFrame from Ray's shared memory object store
# - Opens a new Pandas sheet with that DataFrame as the source
s.send(
f"""
import ray
ray.is_initialized() or ray.init('{ray.runtime_context.get_runtime_context().gcs_address}')
df = ray.get(ray.cloudpickle.loads({serialized_ref}))
vd.push(visidata.PandasSheet('', source=df))
ray.shutdown()
""".encode(
"utf8"
),
)
print(f"Remote command exit code: {s.recv(1024).decode('utf8')}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment