There is no direct way to pass arguments to a notebook as a dictionary or list.
You can work around this limitation by serializing your list as a JSON file and then passing it as one argument.
After passing the JSON file to the notebook, you can parse it with json.loads().
Instructions
Define the argument list and convert it to a JSON file.
- Start by defining your argument list.
args = {'arg_1': 'a', 'arg_2': 'b', 'arg_3': 'c', 'arg_4': 'd', 'arg_5': 'e'}
- Declare the arguments as a dictionary.
- Use json.dumps() to convert the dictionary to JSON.
- Write the converted JSON to a file.
%python import json args = {"arg_1": "a", "arg_2": "b", "arg_3": "c", "arg_4": "d", "arg_5": "e"} file = open("arguments.txt", "w") file.write(json.dumps(args)) file.close() print("Argument file is created.")
Once the argument file is created, you can open it inside a notebook and use the arguments.
- Open the JSON file that contains the arguments.
- Read the contents.
- Use json.loads() to convert the JSON back into a dictionary.
- Now the arguments are available for use inside the notebook.
%python import json file = open("arguments.txt", "r") data = file.read() file.close() args = json.loads(data) print(args)
You can use this example code as a base for your own argument lists.