Announcing klipper-repl: the missing Klipper command line

I’m happy to announce the initial release of klipper-repl, a command line frontend for Klipper that uses the same socket API as Fluidd and Mainsail to provide an efficient G-Code REPL interface.

Its features include:

  • Automatic reconnection if Klipper restarts or is unavailable
  • Support for running G-Code commands as part of shell scripts
  • Multiple G-Code commands per line – use , as a separator
  • Syntax highlighting for both G-Code and user-defined macros
  • Tab autocompletion for user-defined macros
  • M112 emergency stop processing
  • Support for multiple printers via GNU Parallel

Known bugs:

  • Sometimes Python will throw an exception when klipper-repl quits via a KeyboardInterrupt (^C)
  • Probably many more; this works for my (fairly strange) Klipper deployment, but needs testing against other systems

Planned features:

  • A lot more configuration options
  • Some form of live prompt updates with extruder/bed/chamber temperatures during printing
  • An option to display a progress bar while a print is running
  • REPL-specific commands for non-G-Code functionality, like toggling power plugs.
7 Likes

Thank you for your work on this! This looks really useful. It will be a while before I have a chance to test it but I can already think of several ways it would help me.

1 Like

Thank you! Definitely let me know if you run into any install problems when you give it a try – my Klipper host uses NixOS, so the pip instructions in the readme haven’t been tested yet.

I am using CB1. I am not able to install klipper-repl.
Method 1:|
biqu@BTT-CB1:~$ cd klipper-repl/
biqu@BTT-CB1:~/klipper-repl$ pip install klipper-repl
ERROR: Could not find a version that satisfies the requirement klipper-repl
ERROR: No matching distribution found for klipper-repl

Method2:
biqu@BTT-CB1:~/klipper-repl$ pip install .
Processing /home/biqu/klipper-repl
Installing build dependencies … done
Getting requirements to build wheel … done
Preparing wheel metadata … done
Collecting prompt-toolkit<4.0.0,>=3.0.38
Using cached prompt_toolkit-3.0.38-py3-none-any.whl (385 kB)
Collecting pygments<3.0.0,>=2.15.1
Using cached Pygments-2.15.1-py3-none-any.whl (1.1 MB)
ERROR: Package ‘klipper-repl’ requires a different Python: 3.9.2 not in ‘<4.0,>=3.10’

FYI: I am accessing CB1 using sudo minicom -D /dev/ttyUSB0 -b 115200

I’m not familiar with the CB1, but method 2 is failing because the CB1’s version of python is slightly older than what klipper-repl supports. Whichever package manager the CB1’s OS uses may have a newer version available.

First I tried this command as mentioned in Readme.

biqu@BTT-CB1:~nix run github:unjordy/klipper-replpl
error: experimental Nix feature ‘nix-command’ is disabled; use ‘–extra-experimental-features nix-command’ to override

It didnt wqork, so i tried:
biqu@BTT-CB1:~$ nix run --extra-experimental-features ‘flakes nix-command’ github:unjordy/klipper-repl – /tmp/klippy_uds
/nix/store/844w1zapdic2pfq489dp9lz60s4q51j7-bash-5.2-p15/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)

12:41:40 PM ## Disconnected. Waiting for Klipper…

Its disconnecting klipper.

I never tried klipper-repl. Sounds pretty interesting.

Your CB1 is probably running Debian 11 Releases · bigtreetech/CB1 · GitHub.

So your package manager is probably “apt”.

Did you try pip install klipper-repl?

But…

Debian based Linux distributions use “apt” (can be considered as a frontend to dpkg). NixOS uses its own packet manager “Nix”.

I guess, that’s why you run into

I didn’t find requirements for klipper-repl (Python version) here GitHub - unjordy/klipper-repl: The missing Klipper command line. A CLI G-Code REPL for 3D printers running on Klipper firmware.

klipper-repl · PyPI says
Requires: Python >=3.10, <4.0

First, you might check, what Python version is installed on your CB1.

If you have problems with pip install, you might read Installing Packages - Python Packaging User Guide when getting errors using pip install.

Good luck, hcet14

1 Like

biqu@BTT-CB1:~/klipper-repl/result/bin$ ./klipper-repl ~/printer_data/comms/klippy.sock
/nix/store/844w1zapdic2pfq489dp9lz60s4q51j7-bash-5.2-p15/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
10:04:06 AM ## Connected to Klipper at BTT-CB1:/home/biqu/printer_data/comms/klippy.sock
^C or ^D to quit; type M112 for emergency stop
BTT-CB1:/home/biqu/printer_data/comms/klippy.sock*

It is running successfully now. with nix flake

1 Like

dear @unjordy, thanks a lot for klipper-repl!
I’m trying to get it to run for a project, but run into some issues:

my install runs on a rpi4, skr pico (via uart). I have installed klipper and fluidd via kiauh for setting up the printer.cfg. I now installed klipper-repl via pip in a virtual-environment.

what works:
I can send a gcode command form the command line.

klipper-repl printer_data/comms/klippy.sock g1 x20

but I get no feedback.

when running without a gcode command “klipper-repl printer_data/comms/klippy.sock” I get the following error:

(trp) trp@trp1:~ $ klipper-repl printer_data/comms/klippy.sock
09:26:48 pm  ## Connected to Klipper at trp1:printer_data/comms/klippy.sock
                ^C or ^D to quit; type M112 for emergency stop
Traceback (most recent call last):
  File "/home/trp/trp/bin/klipper-repl", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/trp/trp/lib/python3.11/site-packages/klipper_repl/cli.py", line 105, in main
    asyncio.run(run(args))
  File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/trp/trp/lib/python3.11/site-packages/klipper_repl/cli.py", line 75, in run
    finished, unfinished = await asyncio.wait([
                           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/tasks.py", line 415, in wait
    raise TypeError("Passing coroutines is forbidden, use tasks explicitly.")
TypeError: Passing coroutines is forbidden, use tasks explicitly.
sys:1: RuntimeWarning: coroutine 'Event.wait' was never awaited
sys:1: RuntimeWarning: coroutine 'PromptSession.prompt_async' was never awaited
(trp) trp@trp1:~ $ 

would you have any idea what the issue could be?