iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📼

Automating and Recording Julia REPL Sessions with Replay.jl and Asciinema

に公開

Today

Previously, I wrote an article titled "Automating Keyboard Input in the Julia REPL (Replay.jl)".

To put it simply, it's a package that allows Julia to handle REPL input on your behalf based on pre-described tasks. You can make it look like someone is actually tapping away at the keyboard, as shown below.

asciicast

Now, to the Main Point

So, I've managed to automate REPL input for now. The problem is how to record it.
Personally, I thought manual screenshots would be fine once the input was automated, but an issue titled "Can this package also store the recording to a video file?" was raised. We want to automate the recording too, don't we? Yes, we do (・ω'`)... Alright.

In Python

In Python, you can automate (video-like) screenshots using pyautogui and pygetwindow. These allow you to capture screenshots as PIL-format image objects, narrowing the scope to a specified area (like the focused window). If you want to capture a video, you can continuously grab images and save them in a video format using OpenCV.

It Seems asciinema Is Desired

It seems they want to use asciinema. This is a service that records terminal sessions, and there is also a service where you can upload the recording format to share the results on the web. If you are so inclined, you can even self-host it by placing the JS library locally. While it is convenient, I thought it might have been neglected recently... or so I thought.

It turns out v2.1.0 was released in 2021. The previous version was from Sep 08, 2019, so it seems it was the first release in a while.

https://github.com/asciinema/asciinema/releases/tag/v2.1.0

Added official support for Python 3.8 and 3.9

This is good news.

After Some Consideration

I was skeptical about using asciinema because I was under the "misconception" that it could only record shell operations from the moment the terminal starts. I thought that the command julia app.jl used to execute the script would also be recorded, which wouldn't look very cool.
But wait...

Looking at this code, it seems there is an option like the following:

parser_rec.add_argument('-c', '--command', help='command to record, defaults to $SHELL', default=cfg.record_command)

So, I found that by running the following, I could record only the part while julia examples/helloworld/app.jl is actually running.

$ pip3 install asciinema # install `asciinema`
$ asciinema rec output.cast --command "julia examples/helloworld/app.jl"
$ asciinema play output.cast

The asciinema recording below is what I actually obtained by running the above command.

asciicast

You can see how it types accurately and quickly. By the way, the latest version (v0.4 series) now supports automating multi-line inputs, such as function definitions.

You can also do things like this.

asciicast

Conclusion

In conclusion, I have found that it is possible to record execution results by combining it with asciinema.
I also felt like I want to create a video recording version too. Period.

Discussion