Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute the expression and print its value in Emacs python mode

I'm using Emacs python-mode and ipython as my IDE to write the python code. Python-mode provides a nice function "py-execute-line" to run one line code, and I've bound it to key "F7".

So for the following code:

cell = "Human" #  Move the cursor to this line and run it by pressing F7
print cell     #  Move the cursor to this line and run it by pressing F7

I can get the output printed in the ipython buffer.

In [80]: 
In [81]: Human

I'm wondering whether there's more direct way to check the value of "cell" without the print command. Something like move the cursor to the variable, press some key, and then the value is printed in the ipython output buffer.

cell = "Human" #  Move the cursor to this line and run it by pressing F7
cell = "Human" #  Move the cursor to "cell" and print its value by pressing ?? key or function

I have tried function (py-execute-expression-ipython), but there is no output printed ...

Thanks in advance!

like image 984
user3915365 Avatar asked Nov 11 '22 03:11

user3915365


1 Answers

Without highlighting, I use:

(defun python-eval-expression ()
  (interactive)
  (let ((py-command (read-string "Command: ")))
    (save-excursion
      (setq elpy-shell-echo-output t)
      (setq elpy-shell-echo-input t)
      (elpy-shell--with-maybe-echo (python-shell-send-string py-command)))))

Should be easy to adapt to choose the current selection if exists (using

like image 105
guibor Avatar answered Nov 15 '22 13:11

guibor