Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pre_run_cell to modify cell #319

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

felix-dumit
Copy link

This change allows for pre_run_cell events to modify the content of the cell before it is executed.

One use case for this is a lightweight implementation of magic comments, the event can look for a particular pattern and modify the string before it is run.

@kojix2
Copy link
Member

kojix2 commented Jan 14, 2024

Hi @felix-dumit

Thanks for the pull request.
There are not many active maintainers on iruby and sorry for not replying for 2 years.
If possible, we would like to merge the PRs received.
Please provide use cases and screenshots if you have them.

@felix-dumit
Copy link
Author

felix-dumit commented Jan 25, 2024

Hi @kojix2,

Thanks taking a look at this.

The way I am currently using this is as a building block to enable some light-weight magic comment functionality in ruby jupyter notebooks, so I have this code for example that runs during jupyter setup:

IRuby::Kernel.instance.events.register(:pre_run_cell) do |result|
        # magic comment
        if result.raw_cell.start_with?(MAGIC_COMMENT_PREFIX)
          result.raw_cell.delete_prefix!(MAGIC_COMMENT_PREFIX)
          result.raw_cell = wrap_in_staging_call(result.raw_cell)
        end
end

Here's a screenshot showing the difference when running with the magic comment (in my case it makes any queries go to a staging db server, instead of my local one), and without (local)

image

But this simple change in the code would allow any custom code editing on top of the raw cell content before it is actually executed.

@kojix2
Copy link
Member

kojix2 commented Feb 2, 2024

Thank you very much. I understand the use case. However, it looks like IPython does not allow me to change the code using pre_run_cell.

image

What do you think @mrkn?

@kojix2 kojix2 requested a review from mrkn February 2, 2024 02:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants