2

Let say I have two jupyter notebook files called: main and sub

Here are my needs:

  1. Call and run sub from main
  2. Each notebook has its own variables space. For example, if x=1 in main, but x=2 in sub unless exchange info
  3. I can pass certain variables to sub and retrieve certain variables from sub (not all) ← This is very important as I do not want variables to overlap and mess up the code

I have been trying all days with GPT and Copilot looking for an effective solution but:

  1. Using %run -i% or IPython.get_ipython` will just share all variables between the two notebooks
  2. Using nbclients → both GPT and Copilot keep giving wrong answers (i.e. I still cannot pass and receive variables from sub to main)

I know there is another solution is create a defined fucntion on .py file; however since my work at the moment is complex to convert from ipynb file to py file

If anyone have any suggestions, I would really appreciate (a sample code would be x2 appreciate)

Thank you!

4
  • Hi! What kind of problem(s) are you exactly working on? Perhaps a clearer picture would help us! As you've mentioned, I encountered this problem once too, and yes, I did it by importing my other "sub.py" as a "library" in my "main.py" file! Can you perhaps give some context as to why the .ipynb can't ve converted (?) Commented Feb 7 at 2:03
  • @user24758287: it is complex not cannot convertible. I am a faculty in Finance, and I work intensively in research. My research now is too long to put in one jupyter file Commented Feb 7 at 5:01
  • I agree with user24758287 if you cannot make a minimal discussable example it makes it hard to discuss things commonly used with Jupyter and Python that you may be missing. Just a general guide, if it is too bing to put in one Jupyter .ipynb file then you should probably be using Python more and not relying on only .ipynb files. You can always use the .ipynb to mine data you make in scripts or other notebooks but alas without details we are waving out hands. Are you using pickling, %store to exchange data? Are you using Papermill or Jupytext? Commented Feb 7 at 14:12
  • 1
    @Wayne: Actually, I just figure out %run and %store is a great way to solve my need Commented Feb 7 at 17:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.