VS Code#

VS Code, or Visual Studio Code is a code editor (IDE) that is currently very plebiscited because of its many interesting features and plugins that are easily customizable.

There are plenty of existing IDE and this is just a suggestion in case you have no real preference already. But do try it and you may never come back ;).

Installation#

VSCode works natively on every platform (Windows, Linux, macOS).

On your laptop/desktop#

Installation instructions and docs can be found at https://code.visualstudio.com/

Extensions#

To customize your experience, VS Code has a huge list of extensions https://marketplace.visualstudio.com/VSCode.

They can also be accessed and installed from within the IDE in the extension tab as shown here.

You will find below some very useful extensions to get started with code development in Python.

Full online version (linked to github account)#

Starting from fall 2021, a fully online version of VS Code is available at https://vscode.dev/.

Attention

This requires a GitHub or Microsoft account.

Python development#

VS Code is great for developping in Python.

A tutorial can be found here.

To work with its full features, it requires an extension.

The extension includes code hints, navigation, code formatting and linting, test and variable explorer.

Virtualenvs#

VS Code manages the Python virtual environments as well as Python versions that we have created this morning with pyenv.

At the bottom of the window, one can choose the interpreter before running the code.

Jupyter notebooks#

The Python extension also comes with support for Jupyter notebook edition and execution within VS Code.

A Jupyter server is started internally and allows to create notebooks as if your were on a Jupyter notebook or Jupyter lab server.

Integrated terminal#

The editor comes with an integrated terminal that uses your customizations (prompt, colors, etc.) the same way it would render in any terminal.

More information can be found here.

Version control#

VS code comes with native git support.

The most common actions using git can therefore be performed without leaving the editor.

A particular emphasis was made on the GitHub integration (Microsoft product as well ^^) but it work fine with GitLab and can serve our purpose rightfully.

Gitlens#

A useful extension for git in VS Code is GitLens.

This enriches your experience with lots of cool features like the line-by-line blame (who last modified each line), side bar repository management, git command palette, etc.

We recommend installing it.

Remote development#

When you have code on a server that you want to modify without using your terminal with vi or emacs, then the remote development extension is for you.

It will parse your local ~/.ssh/config file and list the servers you have defined there.

You will then be able to instanciate a VS Code window that is connected on the server, and code live there and run your experiment on a more powerful machine.

Hands-on session#

Let’s edit our first project in VSCode.

cd /path/to/my-project
code ./mynewbook

We will edit _config.yml to personalise out jupyter book. Let’s then download some example notebook from astropy examples, save it as astropy.ipynb and add it to _toc.yml as an additional item - file: astropy.ipynb. To run this notebook, astropy library is needed, so add it to requirements.txt and then also install it locally to the virtual environment (to open the built-in terminal in VSCode use hotkey combination Ctrl+Shift+~).

pip install -r requirements.txt

Then, you may want to open and edit a bit the notebook to your taste (at least add a mention to the original file).

After edit, save the file and use the built-in VSCode git capabilities to commit and push your changes. After CI action is finished, we will see the updated static website.