Skip to content

Commit 4a15127

Browse files
authored
Merge pull request #176 from aleaf/main
Fix broken docs; add PRT Notebook; update VS Code exercise
2 parents 7819b63 + 49c96c5 commit 4a15127

10 files changed

Lines changed: 1277 additions & 291 deletions

File tree

docs/source/conf.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@
133133
# a list of builtin themes.
134134
#
135135
html_theme = 'sphinx_rtd_theme'
136-
import sphinx_rtd_theme
136+
#import sphinx_rtd_theme
137137

138-
html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
138+
#html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
139139

140140
# Theme options are theme-specific and customize the look and feel of a theme
141141
# further. For a list of options available for each theme, see the
@@ -146,7 +146,7 @@
146146
# Add any paths that contain custom static files (such as style sheets) here,
147147
# relative to this directory. They are copied after the builtin static files,
148148
# so a file named "default.css" will overwrite the builtin "default.css".
149-
#html_static_path = ['_static']
149+
html_static_path = ['_static']
150150

151151
# Custom sidebar templates, must be a dictionary that maps document names
152152
# to template names.
@@ -289,7 +289,8 @@
289289
source_path / 'part1_flopy/solutions/07-stream_capture_voronoi.ipynb',
290290
source_path / 'part1_flopy/08_Modflow-setup-demo.ipynb',
291291
source_path / 'part1_flopy/09-gwt-voronoi-demo.ipynb',
292-
source_path / 'part1_flopy/10_modpath_particle_tracking-demo.ipynb',
292+
source_path / 'part1_flopy/10a_prt_particle_tracking-demo.ipynb',
293+
source_path / 'part1_flopy/10b_modpath_particle_tracking-demo.ipynb',
293294
# "bonus" notebooks
294295
source_path / 'part0_python_intro/09_b_Geopandas_ABQ.ipynb'
295296
]
@@ -314,4 +315,10 @@
314315
nbsphinx_thumbnails = {
315316
'notebooks/part1_flopy/01-Flopy-intro':
316317
'_images/flopylogo_sm.png',
318+
'notebooks/part0_python_intro/07b_VSCode':
319+
'_images/code-stable.png',
320+
'notebooks/part0_python_intro/0[0-4]*':
321+
'_static/python-logo-only.png',
322+
'notebooks/part0_python_intro/solutions/0[0-4]*':
323+
'_static/python-logo-only.png'
317324
}

docs/source/part0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ Part 0: General Python Exercises
1818
notebooks/part0_python_intro/solutions/08_pandas.ipynb
1919
notebooks/part0_python_intro/09_a_Geopandas.ipynb
2020
notebooks/part0_python_intro/solutions/09_Geopandas__solutions.ipynb
21-
notebooks/part0_python_intro/10a_Rasterio_into.ipynb
21+
notebooks/part0_python_intro/10a_Rasterio_intro.ipynb
2222
notebooks/part0_python_intro/10b_Rasterio_advanced.ipynb
2323
notebooks/part0_python_intro/11_xarray_mt_rainier_precip.ipynb

docs/source/part1.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Part 1 flopy
1414
notebooks/part1_flopy/solutions/07-stream_capture_voronoi.ipynb
1515
notebooks/part1_flopy/08_Modflow-setup-demo.ipynb
1616
notebooks/part1_flopy/09-gwt-voronoi-demo.ipynb
17-
notebooks/part1_flopy/10_modpath_particle_tracking-demo.ipynb
17+
notebooks/part1_flopy/10a_prt_particle_tracking-demo.ipynb
18+
notebooks/part1_flopy/10b_modpath_particle_tracking-demo.ipynb

notebooks/clear_all_notebooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#'07-stream_capture_voronoi.ipynb',
1717
#'08_Modflow-setup-demo.ipynb',
1818
#'09-gwt-voronoi-demo.ipynb',
19-
'10_modpath_particle_tracking-demo.ipynb'
19+
'10b_modpath_particle_tracking-demo.ipynb'
2020
]
2121

2222
if __name__ == "__main__":

notebooks/part0_python_intro/07b_VSCode.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
![](data/code-stable.png)
2+
13
# 07b: VSCode Tutorial
24
In this tutorial we will explore using an integrated development environment (IDE) for python programming. IDEs offer many advantages over Jupyter Notebooks, particularly for developing production code that will be reused many times and shared with others. The advantages of IDEs include:
35
* *Linting* to catch errors, dead code, and other code quality issues
46
* automatic docstring generation and other auto completion
57
* code navigation (jumping to function definitions, etc.)
68
* interactive debugging (including plotting)
79
* support for automated testing and version control
8-
* unlike Notebooks, code is always executed in the same order (as it would be by another user), thereby increasing the chances for reproducibility.
10+
* unlike Notebooks, code is always executed from start to finish (as it would be by another user), thereby increasing the chances for reproducibility.
911

1012
VSCode offers the above advantages, and many other features through Extensions (plugins) that can be created by anyone. VSCode is also free and works well with large files (GB in size). The Live Share plugin allows for real-time code collaboration and debugging with multiple people.
1113

@@ -16,7 +18,8 @@ If you haven't yet, install VS Code [here](https://code.visualstudio.com/downloa
1618
Once VSCode is installed,
1719
* Open ``notebooks/part0_python_intro/solutions/07a_Theis-exercise-solution.ipynb`` in Jupyter
1820
* From the ``File`` menu, select ``Save and Export Notebook as`` and then ``Executable Script``. This will create a python script version of the notebook (``07a_Theis-exercise-solution.py``)
19-
* Save ``07a_Theis-exercise-solution.py`` to the ``notebooks/part0_python_intro`` folder (or copy it there from your Downloads folder).
21+
* Rename this file to ``my_07a_Theis-exercise-solution.py``
22+
* Save ``my_07a_Theis-exercise-solution.py`` to the ``notebooks/part0_python_intro`` folder (or copy it there from your Downloads folder).
2023

2124
### Launching VSCode
2225
* Open a fresh Miniforge prompt from the Start menu and navigate to the root folder for the class (containing the ``AGENDA.md`` file).
@@ -27,22 +30,23 @@ Once VSCode is installed,
2730
### Getting started
2831
See [here](https://code.visualstudio.com/docs/getstarted/userinterface) for an overview of the VSCode user interface.
2932

30-
Once VSCode is launched, click on [the Extensions icon](https://code.visualstudio.com/docs/editor/extension-marketplace) on the activity bar on the left. Install the following extensions:
33+
Once VSCode is launched, click on [the Extensions icon](https://code.visualstudio.com/docs/editor/extension-marketplace) on the Activity Bar on the left. Install the following extensions:
3134
* Python
3235
* Python indent
3336
* Pylance
3437
* Partial Diff
3538
* Jupyter
3639
* Code spell checker
3740
* autoDocstring
41+
* Rainbow CSV
3842

3943
You may find other cool extensions that you want too. A key indicator of an extension's quality is the number of downloads.
4044

4145
### Linting
42-
Now let's open the script we copied earlier (``notebooks/part0_python_intro/07a_Theis-exercise-solution.py``). You can do this from the File Explorer on the left side of the screen.
46+
Now let's open the script we copied earlier (``notebooks/part0_python_intro/my_07a_Theis-exercise-solution.py``). You can do this from the File Explorer on the left side of the screen.
4347

44-
* as you scroll down through the script, note that "ts" is grayed out in the statement ``for ts in t:`` on line 213. This is the linter telling us that this variable is declared but never used. This is "dead code" that we want remove by refactoring.
45-
* similarly, note further down that the variables ``x`` and ``y`` are underlined on lines 253 and 254. The linter is warning us that these variables were never declared. This is because their declarations were wrapped in the ``get_ipython().run_cell_magic`` statement on line 232, in translation of the notebook to a script.
48+
* as you scroll down through the script, note that "ts" is grayed out in the statement ``for ts in t:`` near line 213. This is the linter telling us that this variable is declared but never used. This is "dead code" that we want remove by refactoring.
49+
* similarly, note further down that the variables ``x`` and ``y`` are underlined near lines 253 and 254. The linter is warning us that these variables were never declared. This is because their declarations were wrapped in the ``get_ipython().run_cell_magic`` statement near line 232, in translation of the notebook to a script.
4650

4751
### Cleaning up the script
4852
Let's clean up the script so that
@@ -61,12 +65,14 @@ Let's use Partial Diff to compare our script to the class solution. Assuming the
6165
* Partial Diff will open up a third window showing the differences
6266

6367
### Debugging
64-
* Place a break point anywhere below the first ``theis()`` call in the ``__main__`` part of the script.
68+
* Place a break point anywhere below the first ``theis()`` call in the ``__main__`` part of the script, by clicking to the left of a line number (you should see a red dot).
69+
* If needed, select the ``pyclass` environment as the Python interpreter. Go to ``View --> Command Palette``, then type ``Python: Select Interpreter``. Choose the option with ``(pyclass)`` from the dropdown menu.
70+
* With the Python script (e.g.``07a_Theis-exercise-solution.py``) tab selected, you should see some version numbers followed by ``(pyclass)`` in the bottom right of the VS Code window. Note that you can also click here to change the Python environment.
6571
* Then go to either ``Run --> Start Debugging`` or click on the debug icon in the Activity Bar and choose ``Run and Debug``. Choose ``Python File`` if prompted for a configuration. The debugger should run to the break point.
6672

6773
Often it is prudent to include internal checks in code, regardless of the context. There are a number of ways to do this; a simple one is an ``assert`` statement that checks a condition.
6874

69-
* Add an ``s = `` in front of the first ``theis()`` call to assign the results to a variable, and then right below, the following ``assert`` statement:
75+
* Add an ``s = `` in front of the first ``theis()`` call to assign the results to a variable, and then in the line below, the following ``assert`` statement:
7076
```
7177
assert np.allclose(s, 1.40636669)
7278
```
@@ -77,9 +83,9 @@ Often it is prudent to include internal checks in code, regardless of the contex
7783
#### The debug working directory
7884
Import ``pathlib`` and type ``pathlib.Path.cwd()`` in the debug console. Note that the current directory is the root folder for the class (where we launched VSCode). VSCode is structured around projects, which include everything in a folder that was opened (and any subfolders). By default, the working directory for debugging is set at the root level for the project. We can change this (and other debugging settings), by creating a configuration file called ``launch.json``, which lives inside of a ``.vscode/`` folder at the root level of the project.
7985
80-
* After stopping the debugging session, make a default ``launch.json`` by clicking on the debug icon in the Activity Bar and then ``create a launch.json file``. Choose ``Python File`` if prompted for a configuration. A new tab will open up with ``launch.json``. Add ``"cwd": "${fileDirname}"`` at the bottom (don't forget the preceding comma!) so that the file looks like this:
86+
* After stopping the debugging session, make a default ``launch.json`` by clicking on the debug icon in the Activity Bar and then ``create a launch.json file``. Choose ``Python Debugger`` and then ``Python File`` if prompted for a configuration. A new tab will open up with ``launch.json``. Add ``"cwd": "${fileDirname}"`` at the bottom (don't forget the preceding comma!) so that the file looks like this:
8187
82-
```
88+
```json
8389
{
8490
// Use IntelliSense to learn about possible attributes.
8591
// Hover to view descriptions of existing attributes.
@@ -110,12 +116,22 @@ The Run and Debug view on the left (available via the Activity Bar) shows the cu
110116
During a debug session, as long as matplotlib has been imported, one can make plots via the Debug Console or by putting plotting code in the script.
111117
112118
### Automatic docstring generation
113-
Right click at the beginning of the first line *below* the ``def`` statement for any of the functions and choose ``Generate docstring``. The autoDocstring extension should make a new template for a numpy docstring that includes all of the parameters listed in the function signature.
119+
Right click at the beginning of the first line *below* the ``def`` statement for any of the functions and choose ``Generate docstring``. The autoDocstring extension should make a new template for a docstring that includes all of the parameters listed in the function signature.
120+
121+
Note the format of the docstring, which may be different than the [Numpy-style docstrings](https://numpydoc.readthedocs.io/en/latest/format.html) in the class solution (the most widely-used format for scientific Python). To generate Numpy-style docstrings by default, click on [the Extensions icon](https://code.visualstudio.com/docs/editor/extension-marketplace) on the Activity Bar on the left. Find the autoDocstring extension, right click and select ``Settings``. In the ``Auto Docstring: Docstring Format`` drop-down, select ``numpy``.
114122
115123
### [Code navigation](https://code.visualstudio.com/docs/editor/editingevolved)
116124
In the main part of the script, right click on one of the function calls (e.g. ``theis``) and choose ``Go to Definition``. VSCode should take you to where the function is defined. Right clicking on ``theis`` after the ``def`` and then ``Go to References`` opens a "peek" window with a list of all of the times ``theis()`` is called. Clicking on one of them takes you to that location in the script.
117125
118126
Navigation works across modules and packages too. If you right click on ``np.meshgrid`` in the main part of the script and ``Go to Definition``, VSCode takes you to the relevant code in ``numpy``.
119127
128+
### Code search
129+
VS Code's powerful search capabilities are another key feature.
130+
* To search for text within a script (for example, the ``theis_xy`` function name), select ``Edit --> Find`` or press ``Ctrl+F``.
131+
* You can also highlight text and press ``Ctrl+F`` to auto-fill the search bar.
132+
* ``Edit --> Find in Files`` will quickly search across all files in a project (i.e., everything within the root folder for the class, which we launched VS Code from earlier).
133+
* ``Edit --> Replace in Files`` can be used to quickly refactor variable names across multiple code modules.
134+
* In complex scripts or workflows, **ease of search is a key reason to choose meaningful and unique variable names!**
135+
120136
### Liveshare
121137
We will demo the Live Share extension during the class. In the meantime, you can learn more about it [here](https://code.visualstudio.com/learn/collaboration/live-share).
File renamed without changes.

0 commit comments

Comments
 (0)