Skip to content

Commit

Permalink
Rephrase, and shorten some of the paragraphs in CLI.md and notebooks_…
Browse files Browse the repository at this point in the history
…to_scripts.md
  • Loading branch information
Gabrielgerez committed Jan 5, 2025
1 parent 45f2387 commit 4880bf6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
10 changes: 7 additions & 3 deletions content/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
- Learn how to write code that can change its behaviour based on command-line arguments
```

In the previous section, we learned how to convert a Jupyter notebook into a Python script that is executable from the command line. That example was simple, but it was quite static, every time we wanted to change something in the workflow we would have had to edit the script, which is not very practical with respect to just running the notebook.
In this section we will learn how to change the behaviour of a script based on command-line arguments. This will allow us to run the script with different parameters without having to edit the script every time.
In the previous section, we learned how to convert a Jupyter notebook into a Python script that is executable from the command line. Though we can now run it in the command line, every time we want to change something in the analysis we would need to edit the script.

In this section we will learn how to change the behaviour of a script based on command-line arguments. This will allow us to run the script with different parameters without having to edit the script every time.

This is useful when you want to share your script with other people, as they wont necesarilly need to know what is inside the script to run it.


## Command-line arguments with `argparse`
Command-line arguments are parameters that are passed to a script when running it in the command line.
Expand Down Expand Up @@ -86,4 +90,4 @@ Discussion:
- What would you have to do if you wanted to add more arguments? or a new analysis?
- How would you work with the arguments in for example a slurm submit script?
````
This is not the only way to add command-line arguments to a script. We encourage you to explore other ways to do this, such as using `sys.argv`, `doctopt` or `click`.
This is not the only way to add command-line arguments to a script. We encourage you to explore other ways to do this, such as using `sys.argv`, `doctopt`, `typer` or `click`.
8 changes: 1 addition & 7 deletions content/notebooks_to_scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ We will all at one point find out that notebooks are not the best tool for ever
- Your notebook has exceeded your laptops capabilities and you want to migrate your workflow to a supercomputer with a scheduler such as SLURM
- You run a complex process in the notebook needs to be optimized and profiled with the relevant tools (We will look at this more later in this course)

Sometimes one also finds oneself in some common pitfalls due to deadlines or just poor planning (test code that turns into production code), these can be such as:
- lack of linearity,
- notebook becoming too long
- Attempt at parallelization with duplicate notebooks, which make it hard to spot errors and maintain the code.

I hope I have convinced you that notebooks are not always the best tool for the job. Although scripts are not the perfect tool either, they are still a powerful tool and allow for more flexibility and control.
Let's move on to how we can convert a notebook to a script.
Although scripts are not the perfect tool either, they are still a powerful tool and allow for more flexibility and control.

## Converting a notebook to a script

Expand Down

0 comments on commit 4880bf6

Please sign in to comment.