Skip to content

make %%writefile work w pshnb #6

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

RensDimmendaal
Copy link

@RensDimmendaal RensDimmendaal commented Jul 21, 2025

%%writefile is a built-in magic that we can now also make aware of the pshnb workdir. This PR makes that possible.

@RensDimmendaal RensDimmendaal linked an issue Jul 21, 2025 that may be closed by this pull request
@RensDimmendaal RensDimmendaal changed the title make writefile take on the pshnb cwd. make %%writefile work w pshnb Jul 21, 2025
@RensDimmendaal RensDimmendaal requested a review from jph00 July 21, 2025 11:27
from IPython.paths import get_ipython_dir
from IPython.core.interactiveshell import InteractiveShell
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed some unused imports

@@ -118,11 +114,23 @@ def bash(self, line, cell=None):
raise e from None
if disp and res: print(res)

@magic_arguments()
@argument('-a', '--append', action='store_true', help='Append contents of the cell to an existing file. The file will be created if it does not exist.')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same args as the original %%writefile

@jph00
Copy link
Contributor

jph00 commented Jul 21, 2025

Thanks @RensDimmendaal ! I just added: 4693623 .

With this change, it is now quite easy to ensure that the bash interpreter directory is synchronized with Python's current directory, and you could do this before you run writefile. This way you have the choice as to whether you want to write a file to the current Python or current shell directory. Also, to write to the current shell directory, you can simply use normal bash cat and heredoc.

I'm thinking we should leave it that way rather than shadowing the writefile magic. I'm a bit nervous that shadowing the right file magic might also cause some kind of strange bugs which I am having trouble thinking about but feel like they could get confusing.

wdyt?

@RensDimmendaal
Copy link
Author

Thanks @RensDimmendaal ! I just added: 4693623 .

With this change, it is now quite easy to ensure that the bash interpreter directory is synchronized with Python's current directory, and you could do this before you run writefile. This way you have the choice as to whether you want to write a file to the current Python or current shell directory. Also, to write to the current shell directory, you can simply use normal bash cat and heredoc.

I'm thinking we should leave it that way rather than shadowing the writefile magic. I'm a bit nervous that shadowing the right file magic might also cause some kind of strange bugs which I am having trouble thinking about but feel like they could get confusing.

wdyt?

Whoah! Smart! More concise and more robust.

We could take this one step further:

  1. before running the bash cmd: sync python cwd -> bash cwd
  2. after running the bash cmd: sync bash cwd -> python cwd

Then:

  • Cmds like %cd would also be compatible.
  • Users dont have to keep this concept of the second cwd in their mind when using pshnb

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.

add %%writefile magic
2 participants