Friday, 3 April 2026

How to plot from the data in tmux console using tmux hotkey?

Data scientists often uses `tmux` to run ML training programs that prints out model accuracy scores at each step. One can use `tensorboard` to draw graphs along the way but sometimes, the scores are printed by the library but by the user's code, e.g., GRPO training.

To plot from the data in tmux console using tmux hotkey, we need a few steps:

1. In `/etc/tmux.conf` or `$HOME/.tmux.conf`, put the line for define tmux hotkey:

bind-key p run-shell "tmux capture-pane -p -J -S -10000 | ~/bin/grpo_plot.sh"

-p prints the history to STDOUT instead of tmux buffer
-J disables line wrapping by tmux pane width
-S / -E : starting and ending line
-10000 : -ve number means from the end of the scroll history
bind-key p : bind the hotkey to <PREFIX + p> where PREFIX is CTRL+B by default

2. In the shell script that is launched by tmux (e.g, `grpo_plot.sh` in this case:

  • Either pass the entire STDIN to a python script to process or run `mapfile -t lines` to read all input lines into an array called `lines`.
  • Run `export DISPLAY=$(tmux show-env | grep ^DISPLAY= | cut -d= -f2)` to get the DISPLAY number of the current tmux client and set it for the subsequent Python program.
  • In the Python program, use matplotlib to plot the graph. Optionally, you can use a combination of Linux commands such as grep, awk, sed, etc., the process/filter the data before passing into your Python program.

No comments:

Post a Comment