nithin
nithin

Reputation: 781

Jupyter Notebook : I am unable to arg-parse a value to variable and generate another notebook from command prompt based on that value

I have a Notebook which I call it as Template . I need to pass a variable called ID = 'EF12345'(string value) as an argument based on which my notebook will run and create a new notebook

These are few posts which I referred , I am missing something where I am not to figure out Argparse in Jupyter Notebook throws a TypeError

I cannot do pip install (blocked -- no internet)

Passing command line arguments to argv in jupyter/ipython notebook

Original Code cold_mill_id = 'E126644'

  strip_stats = hive_ctx.sql(query).toPandas()
  strip_stats[strip_stats.cold_mill_id == cold_mill_id]

  sns.set(font_scale=1.2, font='DejaVu Sans')
  fig, ax = plt.subplots(1, 1, figsize=(15, 6))
  xp = df.position
  yp = df.fm_entry_temperature
  p = ax.plot(xp, yp, linestyle='-', label='fm_entry_temperature')
  yp = df.fm_exit_temperature
  p = ax.plot(xp, yp, linestyle='-', label='fm_exit_temperature')
  p = ax.set_title('finishing mill temperature profiles for cold_mill_id=' + 
  cold_mill_id)
  p = ax.set_xlabel('position')
  p = ax.set_ylabel('temperature    (F)')
  p = ax.set_ylim(1650, 2100)
  p = plt.legend()

I added argparse to get value for my cold mill id

   parser.add_argument('--id')
   args = parser.parse_args([])
   args, cold_mill_id = parser.parse_known_args()

ID which I am passing is not working , I am not able to pass ID - which is string and then my code sucks This is how I need

nbconvert --to notebook --execute Template.ipynb 'E12664'

ID which I am passing from Parser is not working , neither can I debug from Command line what is happening fails at certain point in execution when TYPE of ID mismatches with what is expected

Added this piece of code after argparse to check what is getting inside but won't print it in command line

import sys
print(sys.argv)

I am doing something horribly wrong I am clueless What I need is I need to somehow pass this ID to cold_mill_id in jupyter notebook and save those results for new ID as one more notebook

Upvotes: 0

Views: 846

Answers (1)

hpaulj
hpaulj

Reputation: 231325

When I try to run a commandline like yours I get:

1509:~$ jupyter nbconvert --to notebook --execute text.ipynb IPYQX
[NbConvertApp] WARNING | pattern 'IPYQX' matched no files
[NbConvertApp] Converting notebook text.ipynb to notebook
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Writing 921 bytes to text.nbconvert.ipynb

In other words, it view 'IPYQX' as another file that it is supposed to convert. It is not an argument to be passed on to a notebook

With a notebook cell like:

import sys
with open('test.txt', 'w') as f:
    f.write(str(sys.argv))
    f.write('\n')

I get a file like:

1510:~$ cat test.txt
['/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py', '-f', '/tmp/tmpis4wjqdn.json']

This is the sys.argv that was passed to the launcher script to run the notebook. It looks nothing like the command line given to nbconvert.

As has been observed in other SO, we cannot pass command line values to a notebook. Can't you run this code as a python script instead?

Upvotes: 1

Related Questions