astoff / code-cells.el

Emacs utilities for code split into cells, including Jupyter notebooks
GNU General Public License v3.0
180 stars 11 forks source link

see executed command in python buffer #3

Closed mazer-ai closed 2 years ago

mazer-ai commented 2 years ago

Just found this yesterday and already finding it really useful. One quick question/feature-request: any ideas on how to get comint to copy the executed string into the output buffer when you run code-cells-eval into the REPL buffer? It would be really useful to be able to have the command visible above the output, instead of just displayed transiently in the command area.

I feel like I'm missing something obvious here, but after an afternoon pouring through comint.el, python.el and code-cells.el and googling, I just can't figure out how the right way to do it.

Seems like it would be incredibly useful to facilitate using this as a substitute for a full-blown jupyter notebook instance. FWIW -- python-cell.el is in the same boat.

astoff commented 2 years ago

This seems to be something a bit tricky to implement, and probably has to be done separately for each comint. Of course, if someone figures out how to do it in a reasonably clean way, then I'd be happy to merge it.

The emacs-jupyter package provides an option to enable exactly the behavior you descried. It's called jupyter-repl-echo-eval-p. I must say that I used it for a while, and then realized it's not as useful as I had anticipated.

Also, in case you are already using (the unrelased) Emacs 28, there are some improvements in the Python shell. For instance, if you evaluate a compound statement with code-cells-eval or python-shell-send-region, then the value returned by the last expression is printed, just like Jupyter does.

mazer-ai commented 2 years ago

Well, at least glad to know it's not just me or something bizzare about my setup!

I just hacked up a version of python-shell-send-region that sends a print command with the region before actually sending the region to eval. And then set code-cells-eval-region-command to point there. Very fragile, but enough to help me see how useful this will be before spending real time trying to figure out a proper solution.

Right now, I can't run emacs-jupyter without compiling custom emacs -- available emacs for ubunut/mint are not compilled with module support... this is known issue according to the emacs-jupyter issue tracker.

Thanks!

On Sun, Oct 3, 2021 at 1:20 AM Augusto Stoffel @.***> wrote:

This seems to be something a bit tricky to implement, and probably has to be done separately for each comint. Of course, if someone figures out how to do it in a reasonably clean way, then I'd be happy to merge it.

The emacs-jupyter https://github.com/nnicandro/emacs-jupyter package provides an option to enable exactly the behavior you descried. It's called jupyter-repl-echo-eval-p. I must say that I used it for a while, and then realized it's not as useful as I had anticipated.

Also, in case you are already using (the unrelased) Emacs 28, there are some improvements in the Python shell. For instance, if you evaluate a compound statement with code-cells-eval or python-shell-send-region, then the value returned by the last expression is printed, just like Jupyter does.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/astoff/code-cells.el/issues/3#issuecomment-932879413, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATUE7KNTZECMCYTUSXUI56DUE773HANCNFSM5FG24KSA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

mazer-ai commented 2 years ago

This works reasonably well, though it's hard coded for # %% style cells:

(defun my-python-shell-send-region (start end &optional send-main msg) (let ((s (buffer-substring start end))) (setq s (replace-regexp-in-string "# %%$" "" s)) (setq s (replace-regexp-in-string "\n\n" "\n" s)) (setq lines (split-string s "\n")) ;; trim if too many lines (if (> (length lines) 7) (setq s (concat (string-join (seq-subseq lines 0 6) "\n") "\n------- [trimmed] -------\n")) (setq s (concat s "-------------------------\n"))) (with-current-buffer (python-shell-get-buffer) (insert s) (comint-set-process-mark))) (python-shell-send-region start end send-main msg) (with-current-buffer (python-shell-get-buffer) (end-of-buffer)))

I changed code-cells-eval-region-commands to use this for python-mode and been using it the last week or so w/o problems. If this isn't too ugly, I can submit a PR or something.

On an unrelated note -- is there something special that needs to be set up to get the code cell highlighting to work? It's visible in the README, but I can't for the life of me figure out what I need to do to get the cell boundary lines to highlight :-)

On Sun, Oct 3, 2021 at 10:49 AM Jamie Mazer @.***> wrote:

Well, at least glad to know it's not just me or something bizzare about my setup!

I just hacked up a version of python-shell-send-region that sends a print command with the region before actually sending the region to eval. And then set code-cells-eval-region-command to point there. Very fragile, but enough to help me see how useful this will be before spending real time trying to figure out a proper solution.

Right now, I can't run emacs-jupyter without compiling custom emacs -- available emacs for ubunut/mint are not compilled with module support... this is known issue according to the emacs-jupyter issue tracker.

Thanks!

On Sun, Oct 3, 2021 at 1:20 AM Augusto Stoffel @.***> wrote:

This seems to be something a bit tricky to implement, and probably has to be done separately for each comint. Of course, if someone figures out how to do it in a reasonably clean way, then I'd be happy to merge it.

The emacs-jupyter https://github.com/nnicandro/emacs-jupyter package provides an option to enable exactly the behavior you descried. It's called jupyter-repl-echo-eval-p. I must say that I used it for a while, and then realized it's not as useful as I had anticipated.

Also, in case you are already using (the unrelased) Emacs 28, there are some improvements in the Python shell. For instance, if you evaluate a compound statement with code-cells-eval or python-shell-send-region, then the value returned by the last expression is printed, just like Jupyter does.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/astoff/code-cells.el/issues/3#issuecomment-932879413, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATUE7KNTZECMCYTUSXUI56DUE773HANCNFSM5FG24KSA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

astoff commented 2 years ago

The code looks reasonable (you might want to use insert-before-markers instead of insert), but IMO this package is not the right place for that functionality. Emacs itself (python.el) would be a better place.

As to the code cell highlighting, you can customize the code-cells-header-line face. I've recently changed the default value, so if you update the package you may now be satisfied with the default.

astoff commented 2 years ago

Some other packages provide a wiki for users to share configuration snippets and small extensions. If you are interested, I can open a wiki here as well.

mazer-ai commented 2 years ago

Totally your call -- glad to stick it in a wiki. I have it in a fork as well, so not necessary if I'm the only one who'd use it... You might be right, though -- python.el might be better place for this.. Kind of surprised that with all the people using ipython and notebooks for reproducibility this isn't already there.. hard to review the output cells w/o seeing at least a hint of the input...

On Thu, Oct 28, 2021 at 3:09 AM Augusto Stoffel @.***> wrote:

Some other packages provide a wiki for users to share configuration snippets and small extensions. If you are interested, I can open a wiki here as well.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/astoff/code-cells.el/issues/3#issuecomment-953657699, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATUE7KKKF6O6JAE7F6RWKWLUJEOONANCNFSM5FG24KSA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

astoff commented 2 years ago

All right, I opened a Wiki, which should be open for all to edit (let me know otherwise). Thanks!