NEU-Coop-2024 / helical-jupyter

0 stars 0 forks source link

Run your echo program using a custom cell magic #1

Closed etosch closed 1 month ago

etosch commented 1 month ago

Now that you have basic python program that echos and appends content, you will need to implement a call to that program using "custom cell magic," e.g. %% echo_append such that the following requirements are met:

  1. Inside a notebook program, the code should have the following form:
[1] --------------------------------------------------------------------
     | %% echo_append
     | # Some arbitrary code here
     | print('asdf')
     |--------------------------------------------------------------------

and when you run the code, the output should be:

asdf
Success!
  1. Your call must have the custom magic form, i.e., it should not use built-in magics. However, you may want to read more about magics to better understand their purpose, functionality, and capabilities.

  2. Your magic should be accessible from a Python notebook initialized from any location on your filesystem (within reason). You will likely run into path issues along the way. Post any problems you encounter here.

kyang04 commented 1 month ago

image In this example, the notebook is initialized in an arbitrary folder, while the EchoCellMagic.py is located in the /Users/kevin folder. The code provided in the screenshot shows the redirection to the folder where the .py file is located, allowing the Jupyter notebook to load the specified file. This allows the user to create a notebook anywhere in their file system, and make use of the script by changing the folder path in sys.path.append.

etosch commented 1 month ago

Moving forward, please do not close issues until I have had the chance to review new code or comments.

This issue is not yet closed; updating the Python path from inside the notebook environment is not the proper solution. To do this properly, you will need to ensure that the location where the cell magic code lives is accessible through the path that Jupyter Notebook uses upon launch.

Also be sure to document your implementation fully. This means including the versions of Python and Jupyter that you are running. The task will be completed when I can follow the instructions you've written out exactly and reproduce cell 57 without running the content of cell 56. Feel free to use a build system in service of this task.

kyang04 commented 1 month ago

Versions Used: Python 3.12.4 Jupyter Notebook 7.0.8 Python 3 kernel

This solution is incomplete, I was unable to avoid the use of built-in magics to load the custom cell magic into a Juptyer notebook. However, this solution is more preferable than navigating to the script’s path within the notebook, as the user does not have to know the location of the script to load it into a Jupyter notebook. I will continue to look for a solution that meets the requirements.

image

  1. Download the EchoPackage folder and run the pip install . command from the command line in the aforementioned folder.
  2. Open a Jupyter notebook and create a cell with %load_ext EchoCellMagic.EchoCellMagic to initialize the custom cell magic
  3. In a different cell, use the %%echo_append command along with a print statement
etosch commented 1 month ago

For documenting versions used, I recommend including them in a README or some other file in the repository (rather than Github's issue tracker).

I see you're using setup.py. You should look into more modern packaging systems that use pyproject.yaml.

I had in mind the setup that the clingo cell magic uses, but searching for repositories that have mention cell magic, several of the most recent ones require extension registration via load_ext, as documented in your current solution. This seems to be current best practices, so I am fine with having one "extra" step (and you should feel empowered to push back against a requirement if you believe it goes against best practices, provided that you can point to evidence that the alternative solution is more appropriate).

kyang04 commented 1 month ago

https://github.com/ipython/ipython/issues/12838 Yes, I noticed other users ran into similar issues I did - the documentation for magics was unclear as to whether the explicit declaration was required, it implied that @register_line_cell_magic does not need the &load_ext to use the magic, however, I was unable to use @register_line_cell_magic in any case.

It appears that explicit declaration is preferable in these cases, as one user put it: "Personally i think that the explicit registration in load_ext is better; in particular if your magics might have side effect, or be slow to load."