Run magic command not passing variable

Hello everyone, I have many Jupyter notebooks with different ipywidgets programs in them. Now, I wanna make a “Master notebook” and use the %run magic command to run the other scripts in the cells of my master notebook. The problem is that i have to pass a dataframe to the children widgets and it seems like I can’t.

I am running

 %run widgets_program1.py {df}

in a cell, and it doesn’t seem to be passing the variable. It seems he can only pass strings, but even if I do

df_string=dft.to_csv() #in the master notebook
%run widgets_program1.py {df_string}

and then

df=pd.read_csv(StringIO(sys.argv[1]))  #in the children

it doesn’t seem to work, infact if I print the lenght of sys.argv[1] in the children notebook it’s completly wrong (208 vs 800k+). Does anybody have a guess of why?

I didn’t resolve the issue exactly, but for anyone who is wandering, if you want to define the dataframe in the master notebook and then call the children program on the same dataframe you can do it by running

df=pd.Dataframe(......)
%run -i widgets_program1.py #run this in the master

And just calling df in the children program will refer to the same variable.

From here:

“Parameters after the filename are passed as command-line arguments to the program (put in sys.argv). Then, control returns to IPython’s prompt.”

This means it is using your system’s argument passing abilities. Typical command line stuff. See here. So %run widgets_program1.py {df} cannot take a Pandas dataframe and pass it in that way as it is a Python object and your command line system isn’t set up for that.

So it seems you found one way to do what you want by running the script inside the Jupyter namespace.

There are other ways that would allow your script to be able to take an external dataframe or text table, both saved as files, perhaps a pickled dataframe in the former case. However, you’d have to edit your scripts further to handle that. This would allow it to also do this with straight Python and not require users of your script to be working with Jupyter or IPython.

Another option to be able to run your scripts in your master notebook is to structure your scripts so you can import them into a notebook. I have an example of importing the main function of a script here.