{ "cells": [ { "cell_type": "markdown", "id": "8f6bbaa9", "metadata": {}, "source": [ "# VS Code setup\n", "\n", "Now that you know the basics of how Python works \"behind the scenes\", have executed several lines of code within this book, and have installed everything needed to run Python on your computer, let's see how you can start writing Python code within your local IDE, VS Code. \n", "A very useful feature of VS Code is its support of various programming languages - if you decide to work with another language or with several languages in parallel in the future, you can do it all within VS Code.\n", "Let's open your VS Code to start exploring its features while writing your first Python script.\n", "\n", "```{admonition} Can't find VS Code on your Mac?\n", ":class: dropdown, hint\n", "\n", "If you've installed and closed VS Code, and cannot seem to find it anymore on your Mac, press `cmd` + space bar to open Spotlight search, type in \"Visual Studio Code\", and you'll find VS Code. Once it's open, it may be handy to make a **shortcut** to VS Code to make it quickly accessible in the future. To do that, drag VS Code app icon to the left side of the line that separates recently used apps in the Dock (tool bar at the bottom of your screen). For more instructions on the Dock, check the [Mac user guide](https://support.apple.com/en-ie/guide/mac-help/mh35859/mac).\n", "\n", "```" ] }, { "cell_type": "markdown", "id": "ef9e6280", "metadata": {}, "source": [ "## Theme\n", "\n", "When you first start VS Code, you will automatically be asked to choose your theme, i.e., the appearance of your coding environment. Choose whichever one you like the most. Tip: some people find dark mode better for readability, while others praise the light one for helping people with dyslexia (you can read more [here](https://builtin.com/software-engineering-perspectives/dark-mode)). \n", "You can also change the theme at a later point in time and browse for more options by following the instructions [here](https://code.visualstudio.com/docs/getstarted/themes).\n", "\n", "```{figure} ../images/installation/mac/VSCode_Mac_2.png\n", "---\n", "height: 300px\n", "name: \n", "---\n", "```\n" ] }, { "cell_type": "markdown", "id": "f4a5b6f8", "metadata": {}, "source": [ "## Activity bar\n", "\n", "Next, familiarize yourself with VS Code’s activity bar on the left side:\n", "\n", "```{figure} ../images/installation/vscode_start/VSC_start_1.png\n", "---\n", "height: 350px\n", "name: \n", "---\n", "```" ] }, { "cell_type": "markdown", "id": "07812e79", "metadata": {}, "source": [ "## Workspace\n", "\n", "To start working in VS Code, you need to open a folder to set your workspace. We recommend opening a repository in your `Documents`, e.g., `Documents/Nanobiology/Programming_foundations/Getting_started/`. From within VS Code, click on Explorer in the activity bar on the left > Open Folder, and then select `Getting_started/` (or whichever repository you decided to use for coding). Alternatively, you can also open a repository by navigating to File > Open. If prompted whether you trust the authors of the files in this folder, select the “Trust” box and press “Yes, I trust the authors”. " ] }, { "cell_type": "markdown", "id": "7a83050a", "metadata": {}, "source": [ "## Terminal\n", "\n", "Next, press View > Terminal. This will open an additional part in your VS Code workspace, with tabs such as Problems, Output, Debug console, and Terminal. We will get acquainted with these later.\n", "\n", "```{figure} ../images/installation/vscode_start/VSC_start_2.png\n", "---\n", "height: 300px\n", "name: \n", "---\n", "```" ] }, { "cell_type": "markdown", "id": "7078ec5e", "metadata": {}, "source": [ "## New Python file\n", "\n", "To start your first Python script, click on New File. When prompted, select Text file (Built-In). In the bottom status bar, you can see that your file type is plain text.\n", "\n", "```{figure} ../images/installation/vscode_start/VSC_start_3.png\n", "---\n", "height: 350px\n", "name: \n", "---\n", "```\n", "\n", "## Python extension\n", "\n", "As you have noticed, you are asked to select a programming language, so let’s click on “Select a language” and select Python. Notice the change in the status bar. You will also see a pop-up window at the bottom right corner asking if you want to install the recommended ‘Python’ extension from Microsoft for the Python language. Select “Install” and wait for the installation to finish in the Extensions part of the activity bar. If you are already asked to select your interpreter as a prompt on the search box, select `python base`." ] }, { "cell_type": "markdown", "id": "c57e80e6", "metadata": {}, "source": [ "## IntelliSense\n", "\n", "Back in your Python file, start typing “pri”. You will notice that VS Code is trying to read your mind and suggest options of what you may want to type. Among these options is `print`, which is indeed what you wanted to write, so you can go ahead and press `Tab` or `Enter` to fill in the full word. This smart completion is called IntelliSense and is included in the Python extension you installed in the previous step. " ] }, { "cell_type": "markdown", "id": "a69b04e2", "metadata": {}, "source": [ "## Executing code\n", "\n", "Let’s write your full first lines of Python code:\n", "\n", "```\n", "print(“Hello world”)\n", "print(“Goodbye”)\n", "```\n", "\n", "Save this file as `my_first_code.py`and then press Run Python file (the “play” icon on the top right) or right-click > Run Python > Run Python File in Terminal to execute this code. The output will show up in the terminal at the bottom of VS Code. \n", "Note that you can also run selected lines of code if you mark them and then press `Shift`+`Enter` or right-click and select Run Python > Run Selection/Line in Python Terminal. This is convenient for testing a part of the code.\n" ] }, { "cell_type": "markdown", "id": "0d42b2dd", "metadata": {}, "source": [ "## Auto-save\n", "\n", "Now let’s make a change and add an exclamation point to our Hello world:\n", "\n", "`print(“Hello world!”)`\n", "\n", "You will notice a dot appearing next to your file name, indicating that you have some unsaved changes. To automatically save all your changes as soon as you make them, navigate to File > Auto Save.\n" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }