diff --git a/api/extension-guides/images/scm-provider/sourcecontrol-menu.png b/api/extension-guides/images/scm-provider/sourcecontrol-menu.png index 996e0fa6db..8405d46d9d 100644 --- a/api/extension-guides/images/scm-provider/sourcecontrol-menu.png +++ b/api/extension-guides/images/scm-provider/sourcecontrol-menu.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bc9c63a4aeae194bc8ae984d9ac7a3fbcd0f663abb0c6317a78955992e60e780 -size 44782 +oid sha256:7c46bcb741233e33d97a052fa0015c261d62627a47d798e94a5139d615e391fa +size 68157 diff --git a/api/extension-guides/scm-provider.md b/api/extension-guides/scm-provider.md index d4b0bc1975..2b990fc6bc 100644 --- a/api/extension-guides/scm-provider.md +++ b/api/extension-guides/scm-provider.md @@ -133,11 +133,11 @@ When creating them, `SourceControl` and `SourceControlResourceGroup` instances r } ``` -The `scm/sourceControl` menu is located contextually near SourceControl instances: +The `scm/sourceControl` menu is the context menu on each `SourceControl` instance in the **Source Control Repositories** view: ![source control menu](images/scm-provider/sourcecontrol-menu.png) -The `scm/change/title` allows you to contribute commands to the title bar of the [Quick Diff](/api/references/contribution-points#QuickDiffProvider) inline diff editor, described [further ahead](#quick-diff). The command will be passed as arguments the URI of the document, the array of changes within it, and the index of the change which the inline change diff editor is currently focused on. For example, here's the declaration of the `stageChange` Git command which is contributed to this menu with a `when` clause testing that the `originalResourceScheme` [context key](/api/references/when-clause-contexts) equals `git`: +The `scm/change/title` allows you to contribute commands to the title bar of the [Quick Diff](/api/references/vscode-api#QuickDiffProvider) inline diff editor, described [further ahead](#quick-diff). The command will be passed as arguments the URI of the document, the array of changes within it, and the index of the change which the inline change diff editor is currently focused on. For example, here's the declaration of the `stageChange` Git command which is contributed to this menu with a `when` clause testing that the `originalResourceScheme` [context key](/api/references/when-clause-contexts) equals `git`: ```ts async stageChange(uri: Uri, changes: LineChange[], index: number): Promise; diff --git a/api/extension-guides/webview.md b/api/extension-guides/webview.md index dbf1ac62c1..3c5bac300a 100644 --- a/api/extension-guides/webview.md +++ b/api/extension-guides/webview.md @@ -434,7 +434,7 @@ In addition, the **Developer: Reload Webview** command reloads all active webvie Webviews run in isolated contexts that cannot directly access local resources. This is done for security reasons. This means that in order to load images, stylesheets, and other resources from your extension, or to load any content from the user's current workspace, you must use the `Webview.asWebviewUri` function to convert a local `file:` URI into a special URI that VS Code can use to load a subset of local resources. -Imagine that we want to start bundling the cat gifs into our extension rather pulling them from Giphy. To do this, we first create a URI to the file on disk and then pass these URIs through the `asWebviewUri` function: +Imagine that we want to start bundling the cat gifs into our extension rather than pulling them from Giphy. To do this, we first create a URI to the file on disk and then pass these URIs through the `asWebviewUri` function: ```ts import * as vscode from 'vscode'; diff --git a/blogs/2023/03/30/vscode-copilot.md b/blogs/2023/03/30/vscode-copilot.md index 938df1823b..f4c1f6dc68 100644 --- a/blogs/2023/03/30/vscode-copilot.md +++ b/blogs/2023/03/30/vscode-copilot.md @@ -13,7 +13,7 @@ March 30, 2023 by Chris Dias, [@chrisdias](https://twitter.com/chrisdias) **AI did not write this blog post, but it will make your development experiences incredible.** -> **Note**: If you like to learn about the latest GitHub Copilot experience in Visual Studio Code, go to the [AI Tools in VS Code](https://code.visualstudio.com/docs/editor/artificial-intelligence) topic, where you'll find details on the Copilot editor integration and Copilot Chat features such as inline Chat, the dedicated Chat view, and Quick Chat. +> **Note**: If you like to learn about the latest GitHub Copilot experience in Visual Studio Code, go to the [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/editor/github-copilot) topic, where you'll find details on the Copilot editor integration and Copilot Chat features such as inline Chat, the dedicated Chat view, and Quick Chat. There is a lot of buzz, excitement, and some concerns around Artificial Intelligence today. Advancements are happening almost daily, it's hard to keep up. But once you give it a try, you quickly realize what more than a million Copilot users see daily, that this technology does not disappoint, especially with Large Language Models (LLMs) like OpenAI's GPT-3.5/4. @@ -109,7 +109,7 @@ To access the chat experiences (in-editor, Chat view, Quick Chat), you'll need t * A "Chat" icon will appear in the Activity Bar, click on it to open the Chat view. Go ahead, ask Copilot to "write a program to calculate the airspeed velocity of an unladen swallow". * To try out Quick Chat, you can run **Chat: Open Quick Chat** or use the `kb(workbench.action.quickchat.toggle)` keyboard shortcut. -You can learn more about the GitHub Copilot and Copilot Chat extensions in the [AI Tools in VS Code](https://code.visualstudio.com/docs/editor/artificial-intelligence) topic. +You can learn more about the GitHub Copilot and Copilot Chat extensions in the [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/editor/github-copilot) topic. ## Responsible AI diff --git a/build/sitemap.xml b/build/sitemap.xml index 29d128480b..6de69b2160 100644 --- a/build/sitemap.xml +++ b/build/sitemap.xml @@ -226,7 +226,7 @@ 0.8 - https://code.visualstudio.com/docs/editor/artificial-intelligence + https://code.visualstudio.com/docs/editor/github-copilot weekly 0.8 @@ -270,6 +270,11 @@ weekly 0.8 + + https://code.visualstudio.com/docs/editor/custom-layout + weekly + 0.8 + https://code.visualstudio.com/docs/sourcecontrol/overview weekly diff --git a/docs/csharp/cs-dev-kit-faq.md b/docs/csharp/cs-dev-kit-faq.md index 89bc8c744e..dbeaafdbfa 100644 --- a/docs/csharp/cs-dev-kit-faq.md +++ b/docs/csharp/cs-dev-kit-faq.md @@ -257,4 +257,4 @@ To resolve this issue, clear out old folders from within the `obj` folder or cle ### I am not getting whole line completions -Whole line completions are disabled when the [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) extension is enabled to allow you to take advantage of the more advanced [AI completion](/docs/editor/artificial-intelligence.md) capabilities. You can verify that Copilot is enabled by checking if the Copilot logo is present in the lower right corner of VS Code. +Whole line completions are disabled when the [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) extension is enabled to allow you to take advantage of the more advanced [AI completion](/docs/editor/github-copilot.md) capabilities. You can verify that Copilot is enabled by checking if the Copilot logo is present in the lower right corner of VS Code. diff --git a/docs/editor/custom-layout.md b/docs/editor/custom-layout.md new file mode 100644 index 0000000000..9221e2259a --- /dev/null +++ b/docs/editor/custom-layout.md @@ -0,0 +1,239 @@ +--- +Order: 23 +Area: editor +TOCTitle: Custom Layout +ContentId: 71e2c9c1-fb19-469a-9620-877d4b08fb0d +PageTitle: Custom layout of Visual Studio Code +DateApproved: 8/3/2023 +MetaDescription: Visual Studio Code custom user interface layout. +--- +# Custom Layout + +Visual Studio Code comes with a simple user interface and convenient default layout. At the same time, VS Code provides options and settings to let you customize the UI layout to suite your preferences and work style. In this topic, we'll highlight various UI customizations so you can display views, editors, and panels in the way that's most productive for you. + +>**Note**: If you are new to VS Code, you may want to start with the [user interface overview](/docs/getstarted/userinterface.md) or take a look at the [Tips and Tricks](/docs/getstarted/tips-and-tricks.md) article. + +This article starts by discussing [Workbench](#workbench) customizations to rearrange UI elements such as the side bars, views, and panels. Later in the article, we'll cover customization of the [Editor](#editor) region with editor groups, split editors, and editor tabs. + +## Workbench + +### Primary side bar + +The default (Primary) side bar shows views such as the File Explorer, Search, and Source Control on the left of the workbench. If you'd prefer it on the right, you can: + +* Right-click the Activity bar and select **Move Primary Side Bar Right**. +* Run **View: Toggle Primary Side Bar Position** to switch the Primary side bar right and left. +* Set the **Workbench > Side Bar: Location** (`workbench.sideBar.location`) [setting](/docs/getstarted/settings.md) to `right`. + +### Secondary side bar + +By default, VS Code shows all views in the **Primary Side Bar** located to the left of the editor region. If you like another place to display views, you can open the **Secondary Side Bar** to the right and drag and drop views into that side bar. + +This can be useful if you'd like to see two views at the same time, for example, the File Explorer on the left and Source Control view on the right: + +![Source Control view in the Secondary Side bar to the right](images/custom-layout/secondary-side-bar.png) + +To display the Secondary side bar, you can: + +* Run **View: Toggle Secondary Side Bar Visibility** (`kb(workbench.action.toggleAuxiliaryBar)`). +* Check the **View** > **Appearance** > **Secondary Side Bar** menu item. + +The Secondary side bar is initially empty but you can drag and drop views and panels into it and the layout is preserved across your VS Code sessions. + +>**Note**: You can reset views and panels back to their default locations with the **View: Reset View Locations** command. + +### Panel + +The Panel region displays UI elements such as the Problems, Terminal, and Output panels and by default is located under the editor region. + +### Panel position + +You can also move the region to the left or right of the editor with the **Move Panel** commands: + +* **View: Move Panel Left** (`workbench.action.positionPanelLeft`) +* **View: Move Panel Right** (`workbench.action.positionPanelRight`) +* **View: Move Panel To Bottom** (`workbench.action.positionPanelBottom`) + +You can configure these options in the menu under **View** > **Appearance** > **Panel Position**, Panel title bar context menu, or using the new **View: Move Panel** commands. + +![Panel title bar context menu with Panel Position options](images/custom-layout/panel-context-menu-position.png) + +### Panel alignment + +This option lets you configure how far the bottom Panel spans across your window. There are four options: + +* **Center** - This is the default behavior. The panel spans the width of the editor area only. +* **Justify** - The panel spans the full width of the window. +* **Left** - The panel spans from the left edge of the window to the right edge of the editor area. +* **Right** - The panel spans from the right edge of the window to the left edge of the editor area. + +With all Panel alignment options, the Activity Bar is considered the edge of the window. + +You can configure these options in the menu under **View** > **Appearance** > **Align Panel**, Panel title context menu, or using the new **Set Panel Alignment to...** commands. + +![Align Panel options from Panel title context menu](images/custom-layout/panel-alignment-context-menu.png) + +### Maximize Panel size + +You can quickly toggle the Panel region to fill the entire editor area with the **Maximize Panel Size** chevron button in the upper right of the Panel region. The chevron button points downwards in the maximized panel to restore the panel to the original size. + +![Maximize Panel Size button in the upper right of the Panel region](images/custom-layout/maximize-panel-size.png) + +You can also maximize the Panel region via the **View: Toggle Maximized Panel** command. + +> **Note**: Besides customizing the overall Panel region display, individual panels may have their own layout customizations. For example, the Terminal lets you have [multiple open tabs](/docs/terminal/basics.md#managing-terminals) and [split existing terminals](/docs/terminal/basics.md#groups-split-panes). + +### Customize Layout control + +The VS Code title bar also has buttons to toggle the visibility of the main UI elements (Side bars and Panel region). + +![Title bar buttons to toggle main UI elements, with hover on Toggle Panel](images/custom-layout/toggle-UI-visibility.png) + +The rightmost button brings up the **Customize Layout** dropdown, where you can further change the visibility and layout of various UI elements and includes several layout modes: + +![Customize Layout dropdown shown via the Customize Layout button in the title bar](images/custom-layout/customize-layout-dropdown.png) + +The layout modes are: + +* **Full Screen** - Set the editor to fill the full display screen. **View: Toggle Full Screen** (`kb(workbench.action.toggleFullScreen)`). +* **Zen Mode** - Hide all UI except for the editor area. **View: Toggle Zen Mode** (`kb(workbench.action.toggleZenMode)`). +* **Centered Layout** - Centers the editor inside the editor region. **View: Toggle Centered Layout**. + +### Drag and drop views and panels + +VS Code has a default layout of views and panels in the Primary Side bar and Panel region but you can drag and drop views and panels between these regions. For example, you can drag and drop the Source Control view into the Panel region or put the Problems panel into the Primary Side bar: + +![The Source Control view in the Panel region and Problem panel in the Primary Side bar](images/custom-layout/non-default-layout.png) + +>**Note**: Reminder that you can reset a view and panel back to its default location with the **Reset Location** context menu item or all views and panels with the general **View: Reset View Locations** command. + +You can also add views and panels to existing view or panel to create groups. For example, you could move the Output panel to the Explore view group by dragging over the Explorer Activity bar item and then dropping into the view: + +![Output panel moved to the Explorer view group](images/custom-layout/output-in-explorer-group.png) + +You are not limited to using the mouse for moving views and panels. You can also customize layouts via the keyboard with the **View: Move View** and **View: Move Focused View** commands, where dropdowns let you pick the UI element to move and the destination, either a location like the Side bar or Panel region or an existing view or panel to create a group. + +## Tool bars + +Most VS Code views and panels have tool bars displayed on the top right of their UI. For example, the Search view has a tool bar with actions such as **Refresh**, **Clear Search Results**, etc.: + +![Search view tool bar with hover over Clear Search Results action](images/custom-layout/search-view-toolbar.png) + +### Hide items in tool bars + +If you think a tool bar is too busy and you'd like to hide less frequently used actions, you can right-click on any action and select its **Hide** command (for example **Hide 'Clear Search Results'**) or uncheck any of the actions from the dropdown. Hidden actions are moved to the `...` **More Actions** menu and can be invoked from there. + +To restore an action to the tool bar, right-click the tool bar button area and select the **Reset Menu** command or recheck the hidden action. To restore all menus in VS Code, run **View: Reset All Menus** from the Command Palette (`kb(workbench.action.showCommands)`). + +![Search tool bar context menu with Reset menu command](images/custom-layout/reset-tool-bar-menu.png) + +## Editor + +You can customize the layout of the VS Code editor region independent of the workbench user interface. By default, the editor region displays useful features such as the minimap, breadcrumbs, editor tabs, and has optional UI such as Sticky Scroll. You can also adjust the layout of the editors themselves. + +### Minimap and breadcrumbs + +The **View** > **Appearance** menu has a section for customizing the editor region. There you'll find toggles for: + +* **Minimap** - A [visual overview](/docs/getstarted/userinterface.md#minimap) of your current file. **View: Toggle Minimap**. +* **Breadcrumbs** - Display [folder, file, and current symbol](/docs/getstarted/userinterface.md#breadcrumbs) information for the active file. **View: Toggle Breadcrumbs**. +* **Sticky Scroll** - Display nested symbol scopes in the active file. **View: Toggle Sticky Scroll**. + +### Editor groups + +By default, all opened editors go into the same **editor group** and add a new editor tab to the right. You can create new editor groups in order to group similar or related files or allow [side by side editing](/docs/getstarted/userinterface.md#side-by-side-editing) of the same file. Create a new editor group by dragging an editor to the side or using one of the **Split** commands in the context menu to split the current edit and start a new editor group to the left, right, above, or below. + +![Split editor commands in the editor tab context menu](images/custom-layout/split-editor-commands.png) + +The **Split** editor commands are also available from the **View** > **Editor Layout** menu and through the Command Palette. + +If you'd like to go quickly between vertical and horizontal editor group layout, you can use the **Toggle Vertical/Horizontal Editor Layout** command (`kb(workbench.action.toggleEditorGroupLayout)`). + +### Split in group + +You can also split an editor in the same group for side by side editing with the **View: Split Editor in Group** command (`kb(workbench.action.splitEditorInGroup)`). + +When using the split in group feature, there are specific commands for toggling this mode and navigating between the two split editors: + +* **View: Split Editor in Group** - Split the current editor. +* **View: Toggle Split Editor in Group** - Toggle between split mode for the active editor. +* **View: Join Editor in Group** - Go back to a single editor for the active file. +* **View: Toggle Layout of Split Editor in Group** - Toggle between horizontal and vertical layout. + +To navigate between the sides: + +* **View: Focus First Side in Active Editor** - Move focus to the first (left or top) side of split editor. +* **View: Focus Second Side in Active Editor** - Move focus to the second (right or bottom) side. +* **View: Focus Other Side in Active Editor** - Toggle between the split editor sides. + +The **Workbench > Editor: Split in Group Layout** (`workbench.editor.splitInGroupLayout`) [setting](/docs/getstarted/settings.md) lets you set the preferred split editor layout to either horizontal (default) or vertical. + +### Grid layout + +If you'd like more control over the editor layout, you can use the [grid layout](/docs/getstarted/userinterface.md#grid-editor-layout), where you can have multiple rows and columns of editors visible. The **View** > **Editor Layout** menu lists various editor layout options (for example, **Two Columns**, **Three Columns**, **Grid (2x2)**) and you can adjust the editor sizes by grabbing and moving the sash between editors. + +![Editor Grid 2x2 layout with the sash highlighted](images/custom-layout/grid-editor-layout.png) + +### Pinned tabs + +If you'd like an editor to always be visible, you can pin it to the editor tool bar. You can pin an editor tab from either the context menu or using the command **View: Pin Editor** (`kb(workbench.action.pinEditor)`). + +![Pinned editor tab with pin button highlighted](images/custom-layout/pinned-editor-tab.png) + +Pinned tabs help display files that are important to you as: + +* Pinned tabs always appear first before non-pinned tabs. +* They do not scroll out of view if you have many tabs opened. +* They do not close when using editor tab commands such as **Close Others** or **Close All**. +* They do not close even when you exceed a set limit of opened editors. + +Unpin an editor by clicking on the pin icon, using the **Unpin** editor tab context menu item, or the **View: Unpin Editor** command. + +You can choose how you'd like to display pinned editors with the **Workbench > Editor: Pinned Tab Sizing** (`workbench.editor.pinnedTabSizing`) setting. The options are: + +* `normal`: A pinned tab inherits the look of other tabs (default) +* `shrink`: A pinned tab shrinks to a fixed size showing parts of the editor label. +* `compact`: A pinned tab will only show as icon or first letter of the editor label. + +You can also show pinned editor tabs on a separate row above the regular editor tool bar by setting **Workbench > Editor: Pinned Tabs On Separate Row**. You can pin and unpin editors by dragging and dropping their tabs between the two rows. + +### Locked editor groups + +When using multiple editors, it's common to have one or more that you want to always keep visible. The locked editor group feature, where an entire editor group is locked and visible, provides a stable display and any request to open a new editor will create it in another group. You can tell whether an editor group is locked by the lock icon in the editor group tool bar. + +![Locked editor group with lock icon highlighted](images/custom-layout/locked-editor-group.png) + +You can lock an editor group by selecting **Lock Group** from the editor tool bar **More Actions** `...` dropdown or running the **View: Lock Editor Group** command. + +![Lock Group command in the editor tool bar More Actions dropdown](images/custom-layout/lock-group-command.png) + +You can unlock an editor group by clicking on the lock icon or running the **View: Unlock Editor Group** command. + +Locked groups behave differently than unlocked groups: + +* New editors will not open in a locked group unless explicitly moved there (for example, via drag and drop). +* If a new editor skips a locked group, it will either open in the most recently used unlocked group or create a new group to the side of the locked one. +* The locked state of an editor group is persisted and restored across restarts. +* You can lock empty groups as well allowing for a more stable editor layout. + +The primary use case is for [terminals in the editor area](/docs/terminal/basics.md#terminals-in-editor-area). For example, you might want to edit text on the left and have a terminal on the right that is always visible. When a terminal editor is created and moved to the side, it will automatically lock. This means that even when the terminal on the right is focused, opening a file will open it on the left side without needing to manually change focus first. + +Auto locking groups can be configured using the `workbench.editor.autoLockGroups` setting, which defaults to only terminal editors but any editor type can be added to get the same behavior. + +![Auto Lock Groups setting with Terminal checked in the Settings editor](images/custom-layout/autolockgroup-setting.png) + +The commands related to editor group locking: + +* **View: Lock Editor Group** - Lock the active editor group. +* **View: Unlock Editor Group** - Unlock the active locked editor group. +* **View: Toggle Editor Group Lock** - Lock or unlock the active editor group. + +You must have more that one editor group for these commands to be available. + +## Next steps + +Read on to find out about: + +* [Visual Studio Code User Interface](/docs/getstarted/userinterface.md) - A quick orientation to VS Code. +* [Basic Editing](/docs/editor/codebasics.md) - Learn about the powerful VS Code editor. +* [Code Navigation](/docs/editor/editingevolved.md) - Move quickly through your source code. diff --git a/docs/editor/artificial-intelligence.md b/docs/editor/github-copilot.md similarity index 98% rename from docs/editor/artificial-intelligence.md rename to docs/editor/github-copilot.md index 324a49f976..3c79908744 100644 --- a/docs/editor/artificial-intelligence.md +++ b/docs/editor/github-copilot.md @@ -1,13 +1,13 @@ --- Order: 7 Area: editor -TOCTitle: AI Tools +TOCTitle: GitHub Copilot ContentId: 0aefcb70-7884-487f-953e-46c3e07f7cbe PageTitle: Use GitHub Copilot to enhance your coding with AI DateApproved: 10/4/2023 MetaDescription: Enhance your coding with AI-powered suggestions from GitHub Copilot in Visual Studio Code. --- -# AI Tools in VS Code +# GitHub Copilot in VS Code The [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) extension is an AI pair programmer tool that helps you write code faster and smarter. You can use the Copilot extension in VS Code to generate code, learn from the code it generates, and even configure your editor. @@ -15,7 +15,7 @@ The [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub. ## Prerequisites -You'll use the [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) extension to power your AI suggestions in VS Code. +You'll use the [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) extension to power your artificial intelligence (AI) suggestions in VS Code. > Install the GitHub Copilot extension diff --git a/docs/editor/images/custom-layout/autolockgroup-setting.png b/docs/editor/images/custom-layout/autolockgroup-setting.png new file mode 100644 index 0000000000..a8ec789c7a --- /dev/null +++ b/docs/editor/images/custom-layout/autolockgroup-setting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d3a06ee9894beaee1fa038d611bd1e5d94995d6d6b6458c07f6f654144b55f3 +size 143833 diff --git a/docs/editor/images/custom-layout/customize-layout-dropdown.png b/docs/editor/images/custom-layout/customize-layout-dropdown.png new file mode 100644 index 0000000000..c69e745273 --- /dev/null +++ b/docs/editor/images/custom-layout/customize-layout-dropdown.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b6c36848d499d6defc83d781bad5f32edfa5606cebea77b7dbb0d598073b0d1 +size 51920 diff --git a/docs/editor/images/custom-layout/grid-editor-layout.png b/docs/editor/images/custom-layout/grid-editor-layout.png new file mode 100644 index 0000000000..08efd0414c --- /dev/null +++ b/docs/editor/images/custom-layout/grid-editor-layout.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cf3130c5c051324c78b7b92b04ee4953af6e07bf86aa2b4c2943bc7ee8ce07ba +size 351969 diff --git a/docs/editor/images/custom-layout/lock-group-command.png b/docs/editor/images/custom-layout/lock-group-command.png new file mode 100644 index 0000000000..c684df606b --- /dev/null +++ b/docs/editor/images/custom-layout/lock-group-command.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ce85a48fc3f222695a59cb0df917081eff609d20c5459fdf9d2d1a90f41d4802 +size 59839 diff --git a/docs/editor/images/custom-layout/locked-editor-group.png b/docs/editor/images/custom-layout/locked-editor-group.png new file mode 100644 index 0000000000..820a0fbf9e --- /dev/null +++ b/docs/editor/images/custom-layout/locked-editor-group.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2fa981b9bac0fb5f27525d02be186d9f18e274e33086d51097e607ffd9a03391 +size 43955 diff --git a/docs/editor/images/custom-layout/maximize-panel-size.png b/docs/editor/images/custom-layout/maximize-panel-size.png new file mode 100644 index 0000000000..5bdf6151f6 --- /dev/null +++ b/docs/editor/images/custom-layout/maximize-panel-size.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:375465a3616558e083e3fb5feaa7a86731162f5354c6584d9babe2f3be7b4cc4 +size 13108 diff --git a/docs/editor/images/custom-layout/non-default-layout.png b/docs/editor/images/custom-layout/non-default-layout.png new file mode 100644 index 0000000000..55a73636b5 --- /dev/null +++ b/docs/editor/images/custom-layout/non-default-layout.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6b99cb497ce2937357024d4da43cd8a82459c82fa1448d359a321cbb187880b +size 277508 diff --git a/docs/editor/images/custom-layout/output-in-explorer-group.png b/docs/editor/images/custom-layout/output-in-explorer-group.png new file mode 100644 index 0000000000..b859121d6d --- /dev/null +++ b/docs/editor/images/custom-layout/output-in-explorer-group.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a2ff4606fb2b911319c4b6338296cb8d2c26fcc0e3499faf4b570158995610af +size 298571 diff --git a/docs/editor/images/custom-layout/panel-alignment-context-menu.png b/docs/editor/images/custom-layout/panel-alignment-context-menu.png new file mode 100644 index 0000000000..3a2b4f31dd --- /dev/null +++ b/docs/editor/images/custom-layout/panel-alignment-context-menu.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7418ad08711a6a3fb722df7734ec5ac9ec6948d7094fd83547a90c164d747d0a +size 47638 diff --git a/docs/editor/images/custom-layout/panel-context-menu-position.png b/docs/editor/images/custom-layout/panel-context-menu-position.png new file mode 100644 index 0000000000..840f52d5ac --- /dev/null +++ b/docs/editor/images/custom-layout/panel-context-menu-position.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1c32eb44c40bef2eecc898fef7708f90e83126dab1bf883b92cb4acddf9d04e0 +size 48236 diff --git a/docs/editor/images/custom-layout/pinned-editor-tab.png b/docs/editor/images/custom-layout/pinned-editor-tab.png new file mode 100644 index 0000000000..3110457e2f --- /dev/null +++ b/docs/editor/images/custom-layout/pinned-editor-tab.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a6b333b997c83b20124d315b8f6d7267d01e6f1473e24f5916eb7a0d6c91a10f +size 40858 diff --git a/docs/editor/images/custom-layout/reset-tool-bar-menu.png b/docs/editor/images/custom-layout/reset-tool-bar-menu.png new file mode 100644 index 0000000000..29918380e4 --- /dev/null +++ b/docs/editor/images/custom-layout/reset-tool-bar-menu.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8ab7f0df73a5a9a74ad00d35b5c641a91b17160200bcde1dbd1426e38c51c845 +size 57412 diff --git a/docs/editor/images/custom-layout/search-view-toolbar.png b/docs/editor/images/custom-layout/search-view-toolbar.png new file mode 100644 index 0000000000..24dd928ae5 --- /dev/null +++ b/docs/editor/images/custom-layout/search-view-toolbar.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3262b2971f20c8247f4f315159abb7164ff2167331883800227edaeac52d4506 +size 45897 diff --git a/docs/editor/images/custom-layout/secondary-side-bar.png b/docs/editor/images/custom-layout/secondary-side-bar.png new file mode 100644 index 0000000000..5d94be3c30 --- /dev/null +++ b/docs/editor/images/custom-layout/secondary-side-bar.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:43d2a8eb797865fb853a02563abd016f4a8db9226e7dc8a15cd8ef1b6c0255bc +size 360793 diff --git a/docs/editor/images/custom-layout/split-editor-commands.png b/docs/editor/images/custom-layout/split-editor-commands.png new file mode 100644 index 0000000000..cbd2299760 --- /dev/null +++ b/docs/editor/images/custom-layout/split-editor-commands.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5df7f1409b5d4c0c982a7c377465bc4b57556e5fb063b00a40280cf198aabf78 +size 92671 diff --git a/docs/editor/images/custom-layout/toggle-UI-visibility.png b/docs/editor/images/custom-layout/toggle-UI-visibility.png new file mode 100644 index 0000000000..56d622d923 --- /dev/null +++ b/docs/editor/images/custom-layout/toggle-UI-visibility.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ca5bfb5f401e63f2b5838c5b68b9e855b254c5f55ac16680b43be27684b2acfe +size 5235 diff --git a/docs/editor/intellisense.md b/docs/editor/intellisense.md index ea8b593bfc..0a325dac60 100644 --- a/docs/editor/intellisense.md +++ b/docs/editor/intellisense.md @@ -195,7 +195,7 @@ In VS Code, you can enhance your coding with artificial intelligence (AI), such [![GitHub Copilot extension in the VS Code Marketplace](images/intellisense/copilot-extension.png)](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). ## Troubleshooting @@ -213,7 +213,7 @@ IntelliSense is just one of VS Code's powerful features. Read on to learn more: * [Node.js](/docs/nodejs/nodejs-tutorial.md) - See an example of IntelliSense in action in the Node.js walkthrough. * [Debugging](/docs/editor/debugging.md) - Learn how to set up debugging for your application. * [Creating Language extensions](/api/language-extensions/programmatic-language-features.md) - Learn how to create extensions that add IntelliSense for new programming languages. -* [Artificial Intelligence](/docs/editor/artificial-intelligence.md) - Learn how to use AI with GitHub Copilot to enhance your coding. +* [GitHub Copilot in VS Code](/docs/editor/github-copilot.md) - Learn how to use AI with GitHub Copilot to enhance your coding. ## Common questions diff --git a/docs/editor/port-forwarding.md b/docs/editor/port-forwarding.md index ee3f24ce01..d9cff69b15 100644 --- a/docs/editor/port-forwarding.md +++ b/docs/editor/port-forwarding.md @@ -1,5 +1,5 @@ --- -Order: 23 +Order: 24 Area: editor TOCTitle: Port Forwarding ContentId: d7a80c88-c091-4d13-9240-d432c12407a7 diff --git a/docs/editor/vscode-web.md b/docs/editor/vscode-web.md index b6c4dd0ef5..fbcdc7eb1c 100644 --- a/docs/editor/vscode-web.md +++ b/docs/editor/vscode-web.md @@ -175,7 +175,9 @@ Edge and Chrome today support the [File System API](https://developer.mozilla.or ### Browser support -You can use VS Code for the Web in Chrome, Edge, Firefox, and Safari. +You can use VS Code for the Web in the latest versions of Chrome, Edge, Firefox, and Safari. Older versions of each browser may not work - we only guarantee support for the latest version. + +> **Tip:** One way to check the compatible browser version is to look at the version of [Playright](https://playwright.dev/) currently used for testing VS Code and review its supported browser versions. You can find the currently used Playwright version in the VS Code repo's [package.json](https://github.com/microsoft/vscode/blob/main/package.json) file at `devDependencies/@playwright/test`. Once you know the Playwright version, for example `1.37`, you can then review the **Browser Versions** section in their [Release notes](https://playwright.dev/docs/release-notes). Webviews might appear differently or have some unexpected behavior in Firefox and Safari. You can view issue queries in the VS Code GitHub repo to track issues related to specific browsers, such as with the [Safari label](https://github.com/microsoft/vscode/labels/safari) and [Firefox label](https://github.com/microsoft/vscode/labels/firefox). diff --git a/docs/getstarted/userinterface.md b/docs/getstarted/userinterface.md index 9a11794018..b88caaedc7 100644 --- a/docs/getstarted/userinterface.md +++ b/docs/getstarted/userinterface.md @@ -53,6 +53,10 @@ When you have more than one editor open you can switch between them quickly by h >**Tip:** You can resize editors and reorder them. Drag and drop the editor title area to reposition or resize the editor. +### Split in group + +You can also split the current editor without creating a new editor group with the **View: Split Editor in Group** command (`kb(workbench.action.splitEditorInGroup)`). To learn more about this editor mode and specific commands for navigating between the two sides, you can read the section in [Custom Layout](/docs/editor/custom-layout.md#split-in-group) topic. + ## Minimap A Minimap (code outline) gives you a high-level overview of your source code, which is useful for quick navigation and code understanding. A file's minimap is shown on the right side of the editor. You can click or drag the shaded area to quickly jump to different sections of your file. @@ -369,6 +373,7 @@ The `window.restoreWindows` setting tells VS Code how to restore the opened wind Now that you know the overall layout of VS Code, start to customize the editor to how you like to work by looking at the following topics: * [Changing the Theme](/docs/getstarted/themes.md) - Set a Color and/or File Icon theme to your preference. +* [Custom Layout](/docs/editor/custom-layout.md) - Learn more about VS Code's workbench and editor layout customizations. ## Common questions diff --git a/docs/languages/cpp.md b/docs/languages/cpp.md index 36307d1c23..4826b390ec 100644 --- a/docs/languages/cpp.md +++ b/docs/languages/cpp.md @@ -206,7 +206,7 @@ To install support for Remote Development: GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). ## Feedback diff --git a/docs/languages/csharp.md b/docs/languages/csharp.md index 6cdd785105..8b1ab9bf03 100644 --- a/docs/languages/csharp.md +++ b/docs/languages/csharp.md @@ -76,7 +76,7 @@ IntelliSense just works: hit `kb(editor.action.triggerSuggest)` at any time to g GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). ## Snippets for C# diff --git a/docs/languages/java.md b/docs/languages/java.md index 49f72345d3..ba00aa33e9 100644 --- a/docs/languages/java.md +++ b/docs/languages/java.md @@ -161,7 +161,7 @@ One of the key advantages of VS Code is speed. When you open your Java source fi GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). ## Code snippets diff --git a/docs/languages/javascript.md b/docs/languages/javascript.md index 103826e2d6..2d52410df2 100644 --- a/docs/languages/javascript.md +++ b/docs/languages/javascript.md @@ -255,7 +255,7 @@ Set `"javascript.suggestionActions.enabled"` to `false` to disable suggestions. GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). Once you have the Copilot extension installed and enabled, you can test it our for your JavaScript projects. diff --git a/docs/languages/overview.md b/docs/languages/overview.md index 4ef20c0e6f..ad8b3ddab6 100644 --- a/docs/languages/overview.md +++ b/docs/languages/overview.md @@ -33,7 +33,7 @@ Click on any linked item to get an overview of how to use VS Code in the context The richness of support varies across the different languages and their extensions: * Syntax highlighting and bracket matching -* Smart completions (IntelliSense, Artificial Intelligence with [GitHub Copilot](/docs/editor/artificial-intelligence.md)) +* Smart completions (IntelliSense, Artificial Intelligence with [GitHub Copilot](/docs/editor/github-copilot.md)) * Linting and corrections * Code navigation (Go to Definition, Find All References) * Debugging @@ -47,7 +47,7 @@ In VS Code, you can enhance your coding with artificial intelligence (AI), such [![GitHub Copilot extension in the VS Code Marketplace](images/overview/copilot-extension.png)](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). ## Change the language for the selected file diff --git a/docs/languages/python.md b/docs/languages/python.md index cd326978e5..b54c245d10 100644 --- a/docs/languages/python.md +++ b/docs/languages/python.md @@ -63,7 +63,7 @@ IntelliSense quickly shows methods, class members, and documentation as you type GitHub Copilot provides suggestions for languages beyond Python and a wide variety of frameworks, including JavaScript, TypeScript, Ruby, Go, C# and C++. -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). ## Linting diff --git a/docs/python/debugging.md b/docs/python/debugging.md index 02ee141f7e..bafcf80a39 100644 --- a/docs/python/debugging.md +++ b/docs/python/debugging.md @@ -346,13 +346,13 @@ Specifies the mode in which to start debugging: Provides the fully qualified path to the python program's entry module (startup file). The value `${file}`, often used in default configurations, uses the currently active file in the editor. By specifying a specific startup file, you can always be sure of launching your program with the same entry point regardless of which files are open. For example: ```json -"program": "/Users/Me/Projects/PokemonGo-Bot/pokemongo_bot/event_handlers/__init__.py", +"program": "/Users/Me/Projects/MyProject/src/event_handlers/__init__.py", ``` -You can also rely on a relative path from the workspace root. For example, if the root is `/Users/Me/Projects/PokemonGo-Bot` then you can use the following example: +You can also rely on a relative path from the workspace root. For example, if the root is `/Users/Me/Projects/MyProject` then you can use the following example: ```json -"program": "${workspaceFolder}/pokemongo_bot/event_handlers/__init__.py", +"program": "${workspaceFolder}/src/event_handlers/__init__.py", ``` ### `module` diff --git a/docs/python/editing.md b/docs/python/editing.md index 8c141dc596..9374f9734c 100644 --- a/docs/python/editing.md +++ b/docs/python/editing.md @@ -70,7 +70,7 @@ You can also customize the general behavior of autocomplete and IntelliSense, ev GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). ## Navigation diff --git a/docs/python/formatting.md b/docs/python/formatting.md index ab8fd84e48..dbc9fdc16a 100644 --- a/docs/python/formatting.md +++ b/docs/python/formatting.md @@ -14,7 +14,32 @@ Formatting makes source code easier to read by human beings. By enforcing partic [Linting](/docs/python/linting.md) helps to prevent errors by analyzing code for common syntactical, stylistic, and functional errors and unconventional programming practices. Although there is a little overlap between formatting and linting, the two capabilities are complementary. -The Python extension supports source code formatting through formatter extensions, such as [autopep8](https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8) and [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter). +## Choose a formatter + +Install the formatting tool of your choice from the VS Code [Marketplace](https://marketplace.visualstudio.com/vscode). + +Microsoft publishes the following formatting extensions: + +| Formatter | Extension | +| ------ | ------------------------------------------------------------------------------- | +| autopep8 | [https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8](https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8) | +| Black formatter | [https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter) | + +Formatter extensions offered by the community: + +| Formatter | Extension | +| ------ | ---------------------------------------------------------------------- | +| Ruff | [https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) | +| yapf | [https://marketplace.visualstudio.com/items?itemName=eeyore.yapf](https://marketplace.visualstudio.com/items?itemName=eeyore.yapf) | + +Furthermore, below are formatter extensions that support import sorting: + +| Formatter | Extension | +| ------ | ---------------------------------------------------------------------- | +| Ruff | [https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) | +| isort | [https://marketplace.visualstudio.com/items?itemName=ms-python.isort](https://marketplace.visualstudio.com/items?itemName=ms-python.isort) | + +> **Note**: If you don't find your preferred formatter in the table above or in the Marketplace, you can add support for it via an extension. You can use the [Python Extension Template](/api/advanced-topics/python-extension-template.md) to integrate new Python tools into VS Code. ## Set a default formatter @@ -36,6 +61,20 @@ For example, to set Black Formatter as the default formatter, add the following } ``` +In order to set a formatter extension as an import sorter, you can set your preference under `"editor.codeActionsOnSave"` in your User `settings.json` file or your Workspace `settings.json` file, under a `[python]` scope. You can open these `settings.json` files using the **Preferences: Open User Settings (JSON)** and **Preferences: Open Workspace Settings (JSON)** commands respectively. This will enable import sorting on save for all Python files. + +For example, to set Ruff as your preferred import sorter, you can add the following setting to your User `settings.json` or your Workspace `settings.json` file: + +```json +{ + "[python]": { + "editor.codeActionsOnSave": { + "source.organizeImports.ruff": true + } + } +} +``` + ## Format your code You can format your code by right-clicking on the editor and selecting **Format Document**, or by using the `kb(editor.action.formatDocument)` keyboard shortcut. diff --git a/docs/python/images/fastapi-tutorial/create_environment_interpreters_list.png b/docs/python/images/fastapi-tutorial/create_environment_interpreters_list.png new file mode 100644 index 0000000000..9a05d20484 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/create_environment_interpreters_list.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:08b85c291938d11dbc09b8c249ca5f2414ed34c32cd1a64ccd2345c4259e1ec2 +size 63969 diff --git a/docs/python/images/fastapi-tutorial/create_environment_select_requirements.png b/docs/python/images/fastapi-tutorial/create_environment_select_requirements.png new file mode 100644 index 0000000000..5e270afddb --- /dev/null +++ b/docs/python/images/fastapi-tutorial/create_environment_select_requirements.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:196b256b81ffff5a73ba9c22e6ed33746b5baa2c83500044dd4a7b652242e9be +size 11790 diff --git a/docs/python/images/fastapi-tutorial/debugger_breakpoint.png b/docs/python/images/fastapi-tutorial/debugger_breakpoint.png new file mode 100644 index 0000000000..3ebe76ccbe --- /dev/null +++ b/docs/python/images/fastapi-tutorial/debugger_breakpoint.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:254aabb59c5136fe9a6d675cf8e3cc4b4f376f998bd71548e5d5fae273c9c9ab +size 56761 diff --git a/docs/python/images/fastapi-tutorial/devcontainer_indicator.png b/docs/python/images/fastapi-tutorial/devcontainer_indicator.png new file mode 100644 index 0000000000..1445253115 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/devcontainer_indicator.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1504abb764d3f481295bc72bbc093a47d2e8632f4e708fb5be48b3d11f885baf +size 2673 diff --git a/docs/python/images/fastapi-tutorial/devcontainer_python_interpreter.png b/docs/python/images/fastapi-tutorial/devcontainer_python_interpreter.png new file mode 100644 index 0000000000..66638ca42e --- /dev/null +++ b/docs/python/images/fastapi-tutorial/devcontainer_python_interpreter.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:48db8cf8f8df07ce3aa9e5cfde8f62955611853fb973ecb8d3ad0dedd2dbcb15 +size 4747 diff --git a/docs/python/images/fastapi-tutorial/devcontainers_python3.png b/docs/python/images/fastapi-tutorial/devcontainers_python3.png new file mode 100644 index 0000000000..091b728e5a --- /dev/null +++ b/docs/python/images/fastapi-tutorial/devcontainers_python3.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7f7b988a65b889e605e306332619a604b00057a58798d42b09c4a412dbc5c2c6 +size 24334 diff --git a/docs/python/images/fastapi-tutorial/devcontainers_redis_server_feature.png b/docs/python/images/fastapi-tutorial/devcontainers_redis_server_feature.png new file mode 100644 index 0000000000..e94e982af7 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/devcontainers_redis_server_feature.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:93e483c3be8c8656be0a487c9cd99538717c0743956a637737ad92d513fb39b8 +size 63800 diff --git a/docs/python/images/fastapi-tutorial/dictionarybased_github_branch.png b/docs/python/images/fastapi-tutorial/dictionarybased_github_branch.png new file mode 100644 index 0000000000..5e64f31955 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/dictionarybased_github_branch.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8c2fda314724aba70ed5073f423386d147acf1fec826bb5b3300d8bb660b0a94 +size 33796 diff --git a/docs/python/images/fastapi-tutorial/fastapi_add_redis_quickfix.png b/docs/python/images/fastapi-tutorial/fastapi_add_redis_quickfix.png new file mode 100644 index 0000000000..7258e6ecd3 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_add_redis_quickfix.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c2bfec85cf05586b7650310783dc60ac50414923d87c83d7c237353d4f68b93a +size 24834 diff --git a/docs/python/images/fastapi-tutorial/fastapi_breakpoint_hit.png b/docs/python/images/fastapi-tutorial/fastapi_breakpoint_hit.png new file mode 100644 index 0000000000..6ea145dd81 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_breakpoint_hit.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:659e00f686058091117a46183f17c6780df61ba32ad8d2f4dd73749665d6c74c +size 57572 diff --git a/docs/python/images/fastapi-tutorial/fastapi_debug_config_option.png b/docs/python/images/fastapi-tutorial/fastapi_debug_config_option.png new file mode 100644 index 0000000000..3ec9b7d629 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_debug_config_option.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bd410b1ce6d9a80d30a161de3ec148ba171c04c2d7746ce2bce6f5045ca941f0 +size 89173 diff --git a/docs/python/images/fastapi-tutorial/fastapi_debug_terminal.png b/docs/python/images/fastapi-tutorial/fastapi_debug_terminal.png new file mode 100644 index 0000000000..c69a6ce9d9 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_debug_terminal.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7a78119695dc51f92a6a8b88b67d36ae0c57ccef730609d388adc2775768fe82 +size 217635 diff --git a/docs/python/images/fastapi-tutorial/fastapi_debugger_variables.png b/docs/python/images/fastapi-tutorial/fastapi_debugger_variables.png new file mode 100644 index 0000000000..86ac656cec --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_debugger_variables.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:98be66f161eddec28603d3f32db0ba91083fb3a4ca7fb74b59f284ce2a049b97 +size 87337 diff --git a/docs/python/images/fastapi-tutorial/fastapi_evaluate_debug_console.png b/docs/python/images/fastapi-tutorial/fastapi_evaluate_debug_console.png new file mode 100644 index 0000000000..c23cbfdcab --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_evaluate_debug_console.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6654c71bf9e2d1d37fd90e2869bf6f838695f5570162c1e8489c3f836992a6f6 +size 247921 diff --git a/docs/python/images/fastapi-tutorial/fastapi_execute_button.png b/docs/python/images/fastapi-tutorial/fastapi_execute_button.png new file mode 100644 index 0000000000..1b6a3415cb --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_execute_button.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bcf0a0cb9d61d1eb83ba24af33ba8ee98fb95d813724e45dc429b16c8f5481bd +size 94785 diff --git a/docs/python/images/fastapi-tutorial/fastapi_first_swagger_page.png b/docs/python/images/fastapi-tutorial/fastapi_first_swagger_page.png new file mode 100644 index 0000000000..432fb6ae42 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_first_swagger_page.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6e70ddc3bd40d7bfa4a87f88d79e6e66cb4290636dc0a9c4320481a2d9e7854e +size 52787 diff --git a/docs/python/images/fastapi-tutorial/fastapi_tryitout_button.png b/docs/python/images/fastapi-tutorial/fastapi_tryitout_button.png new file mode 100644 index 0000000000..627f764e1d --- /dev/null +++ b/docs/python/images/fastapi-tutorial/fastapi_tryitout_button.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d8695767ed077096e3dfef915f67d621712c6bd3d508a6b0de28e8b29934f308 +size 60211 diff --git a/docs/python/images/fastapi-tutorial/function_and_variable_return_type_hint_settings.png b/docs/python/images/fastapi-tutorial/function_and_variable_return_type_hint_settings.png new file mode 100644 index 0000000000..f0346f686d --- /dev/null +++ b/docs/python/images/fastapi-tutorial/function_and_variable_return_type_hint_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9601216cc1b14b32ad4ff4b5571529c022e396f142fa8bdf6890e892e9ac7784 +size 39279 diff --git a/docs/python/images/fastapi-tutorial/helloworld_browser.png b/docs/python/images/fastapi-tutorial/helloworld_browser.png new file mode 100644 index 0000000000..8f26b9a4ca --- /dev/null +++ b/docs/python/images/fastapi-tutorial/helloworld_browser.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8644a576287caa5574ffb7f8bed7b0f2bfd0220808a13d0dd797f1f497035537 +size 22309 diff --git a/docs/python/images/fastapi-tutorial/pylance_inlay_hints.png b/docs/python/images/fastapi-tutorial/pylance_inlay_hints.png new file mode 100644 index 0000000000..83aa97692d --- /dev/null +++ b/docs/python/images/fastapi-tutorial/pylance_inlay_hints.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:77f7778e46d2e714ac19a635dd0ae8a8438f6fff116a822f96b67ade263c8570 +size 184339 diff --git a/docs/python/images/fastapi-tutorial/pylance_redis_typehint.png b/docs/python/images/fastapi-tutorial/pylance_redis_typehint.png new file mode 100644 index 0000000000..ee1db229f8 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/pylance_redis_typehint.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b82a5cc0bbc819d2bc29c501022a7e0dd2e90ef36f58b38b7ca56a05ea0d6a0f +size 19234 diff --git a/docs/python/images/fastapi-tutorial/type_checking_mode_setting.png b/docs/python/images/fastapi-tutorial/type_checking_mode_setting.png new file mode 100644 index 0000000000..2265c2fe83 --- /dev/null +++ b/docs/python/images/fastapi-tutorial/type_checking_mode_setting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1d29bc166be1c978481192d7ee811f9739cea1bebdefbadd072674cc97d31b90 +size 32421 diff --git a/docs/python/python-on-azure.md b/docs/python/python-on-azure.md index 403bdccb09..bae1f1b17e 100644 --- a/docs/python/python-on-azure.md +++ b/docs/python/python-on-azure.md @@ -1,5 +1,5 @@ --- -Order: 12 +Order: 13 Area: python TOCTitle: Deploy Python Apps ContentId: 12bf713e-5f20-46ac-81bb-8e05565aba3a diff --git a/docs/python/python-web.md b/docs/python/python-web.md index bb3117c7a1..552d00ece8 100644 --- a/docs/python/python-web.md +++ b/docs/python/python-web.md @@ -1,5 +1,5 @@ --- -Order: 13 +Order: 14 Area: python TOCTitle: Python in the Web ContentId: 366e4bbf-fa87-4813-9dfc-6c831b20a4d2 diff --git a/docs/python/settings-reference.md b/docs/python/settings-reference.md index a6aff8ded1..52935b693b 100644 --- a/docs/python/settings-reference.md +++ b/docs/python/settings-reference.md @@ -1,5 +1,5 @@ --- -Order: 14 +Order: 15 Area: python TOCTitle: Settings Reference ContentId: d256dc5c-95e9-4c02-a82f-947bf34a3517 diff --git a/docs/python/tutorial-create-containers.md b/docs/python/tutorial-create-containers.md index 7abe394dd6..96bdaffdff 100644 --- a/docs/python/tutorial-create-containers.md +++ b/docs/python/tutorial-create-containers.md @@ -1,5 +1,5 @@ --- -Order: 11 +Order: 12 Area: python TOCTitle: Create containers ContentId: 4e45a3f6-b72d-4647-82a5-22f7ee593d47 diff --git a/docs/python/tutorial-fastapi.md b/docs/python/tutorial-fastapi.md new file mode 100644 index 0000000000..dc54465c31 --- /dev/null +++ b/docs/python/tutorial-fastapi.md @@ -0,0 +1,660 @@ +--- +Order: 10 +Area: python +TOCTitle: FastAPI Tutorial +ContentId: 0d32bced91aa5c2ee5696fc7995370ae +PageTitle: Python and FastAPI Tutorial in Visual Studio Code +DateApproved: +MetaDescription: Python FastAPI tutorial showing IntelliSense and debugging support in Visual Studio Code, the best Python IDE. +--- +# FastAPI Tutorial in Visual Studio Code + +[FastAPI](https://fastapi.tiangolo.com/) is a modern high-performant web framework for building APIs with Python. It is designed to make it easy to build APIs quickly and efficiently while providing features like automatic validation, serialization, and documentation of your API, making it a popular choice for building web services and microservices. + +In this FastAPI tutorial, we will create a grocery list app using FastAPI. By the end of the tutorial, you will understand how to work with FastAPI in the VS Code terminal, editor, and debugger. This tutorial is not a FastAPI deep dive. For that, you can refer to the [official FastAPI documentation](https://fastapi.tiangolo.com/). + +If this is your first time using Python, we recommend you to start with our [Python tutorial](python-tutorial.md) to get familiar with the language and VS Code's Python support. This tutorial is more suited for those who are already familiar with Python and want to learn how to work with FastAPI in VS Code. + +The completed code project from this FastAPI tutorial can be found on GitHub: [python-sample-vscode-fastapi-tutorial](https://github.com/microsoft/python-sample-vscode-fastapi-tutorial). + +If you have any problems, you can search for answers or ask a question on the [Python extension Discussions Q&A](https://github.com/microsoft/vscode-python/discussions/categories/q-a). + +## Set up the project +There are different ways you can set up your project for this tutorial. We will cover how you can set it up in [GitHub Codespaces](#github-codespaces) and in [VS Code on your local machine](#locally-in-vs-code). + +### GitHub Codespaces +You can set up this project to develop in [GitHub Codespaces](https://github.com/features/codespaces), where you can code, debug, and run your app remotely in a Codespace. A Codespace allows you to access a fully-configured development environment hosted in the cloud, eliminating the need for local setup. This environment includes your project's dependencies, tools, and extensions, ensuring a consistent and reproducible development experience. It streamlines collaboration by providing real-time editing, integrated version control, and easy access to debugging and testing tools, all while maintaining the security and reliability of your project. + +> Note: All GitHub.com accounts have a monthly quota of free use of GitHub Codespaces included in the Free or Pro plan. For more information, see "[About billing for GitHub Codespaces](https://docs.github.com/en/billing/managing-billing-for-github-codespaces/about-billing-for-github-codespaces)". + +To set it up for this tutorial, navigate to [this project's GitHub repository](https://github.com/microsoft/python-sample-vscode-fastapi-tutorial). This Codespace will contain all the necessary configurations and dependencies to quickly get started with FastAPI development. + +To get the most of this tutorial, select the **dictionarybased** branch: +![dictionarybased branch selected in the python-sample-vscode-fastapi-tutorial GitHub repo.](images/fastapi-tutorial/dictionarybased_github_branch.png) + +Then, select **Code** > **Codespaces** > **Create Codespace on \** branch to create and open a Codespace for your project. + +Once you're done, you can continue with the [Replace the database](#replace-the-database) section below. + +### Locally in VS Code +To successfully complete this tutorial in [VS Code](https://code.visualstudio.com/), you need to first setup your Python development environment. Specifically, this tutorial requires: +- Python 3 (check the [installation guide](python-tutorial.md#install-a-python-interpreter) if you don’t have it installed) +- [Python extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python) (For additional details on installing extensions, see [Extension Marketplace](https://code.visualstudio.com/docs/editor/extension-marketplace)) + +In this section, we will create a folder to be opened as a workspace in VS Code, set up a Python virtual environment, and install the project’s dependencies. + +1. In your file system, create a project folder for this tutorial, such as `groceries-plugin`. +2. Open this new folder in VS Code (**File** > **Open Folder…** or `kb(workbench.action.files.openFolder)`). +3. When the [Workspace Trust](/docs/editor/workspace-trust) prompt shows up, click on the "Yes, I trust the authors" button to allow the workspace to access necessary resources and extensions. You can learn more about Workspace Trust in the [documentation](/docs/editor/workspace-trust). + +Now, let’s create a `requirements.txt` file listing the dependencies we wish to install for the application. The `requirements.txt` file is a common practice in Python development, used to specify the libraries that your project relies on and their versions. This file helps ensure that anyone working on the project can recreate a similar development environment, making it a convenient component for maintaining consistency across different development environments. + +We will want to install FastAPI for creating the app, uvicorn to work as the server, and Redis and type-redis for handling data storage and interacting with a Redis database. + +4. Create a new file in VS Code (**File** > **New Text File** or `kb(workbench.action.files.newUntitledFile)`). +5. Add the following content to it: + ``` + fastapi + redis + types-redis + uvicorn + ``` +6. Save the file (`kb(workbench.action.files.save)`) and name it `requirements.txt` +7. Create a virtual environment by opening the Command Palette (`kb(workbench.action.showCommands)`) and running the **Python: Create Environment** command. + > Note: this step may take a couple of minutes to complete. +8. When asked by the environment type, select **Venv**: +![Drop down with "Venv" or "Conda" as options for environments that can be created with the Python: Create Environment command.](images/environments/create_environment_dropdown.png) + +9. Then select the latest version of Python available on your machine: +![List of available global environments that can be used to create a virtual environment.](images/fastapi-tutorial/create_environment_interpreters_list.png) + +10. Select the `requirements.txt` file from the drop-down list, so the dependencies are automatically installed, and then select "OK": +![Check box selected to install dependencies from requirements.txt file](images/fastapi-tutorial/create_environment_select_requirements.png) + +The virtual environment will be created, the dependencies will be automatically installed and the environment will be selected for your workspace to be used by the Python extension. You can confirm it's been selected by checking the bottom right corner of VS Code: + +![Environment in the status bar](images/shared/environment-in-status-bar.png) + +> Note: If you don't see the newly created environment information on the status bar, you can click on the Python interpreter indicator (or run the **Python: Select Interpreter** command from the Command Palette) and manually select the virtual environment. + +## Start Coding + +Let’s create the application! +1. Create a new Python file by clicking on **File** > **New File…** (`kb(workbench.action.files.newFile)`), and then select "Python File". +2. Save it as `main.py` (`kb(workbench.action.files.saveAs)`) in the `groceries-plugin` folder. +3. Add the following code to `main.py` and save the file: + ```python + from fastapi import FastAPI + + app = FastAPI() + + @app.get("/") + def root(): + return {"message": "Hello World"} + ``` +4. Run the code by starting up the debugger (`kb(workbench.action.debug.start)`) +5. From the drop-down menu, select the **FastAPI** configuration option from the drop-down list: +![Drop down with debugger configuration options, with FastAPI being highlighted.](images/fastapi-tutorial/fastapi_debug_config_option.png) + + This will invoke uvicorn to start the application server through the debugger, which will allow us to step through the code to inspect its behavior. You will see something like the following in the terminal: + + ![Uvicorn server running message displayed in the terminal, with an URL to access the app.](images/fastapi-tutorial/fastapi_debug_terminal.png) + +6. `Ctrl` + click the http://127.0.0.1:8000/ URL in the terminal to open your default browser to that address: +![Hello World message displayed in the browser.](images/fastapi-tutorial/helloworld_browser.png) + + Congrats! Your FastAPI app is up and running! + +7. Stop the debugger by clicking on the stop icon in the debug toolbar, or through `kb(workbench.action.debug.stop)`. + +## Create a model for grocery list items +Now that we have the FastAPI app working, we can define our grocery list items by using [Pydantic](https://docs.pydantic.dev/latest/), which is a data validation and parsing library that integrates seamlessly with FastAPI. Pydantic allows you to define data models using Python classes with [type hints](https://docs.python.org/3/library/typing.html) for automatic validation and parsing of incoming data (which are called payloads) in API requests. + +Let's create a model for our grocery list items. We will use the `ItemPayload` model to define the data structure of the items we will be adding to the grocery list. This model will have three fields: `item_id`, `item_name`, and `quantity`. + +1. Create a new Python file by clicking on **File** > **New File…** (`kb(workbench.action.files.newFile)`), and then select "Python File". +2. Add the following lines to the file, and then save it in the `groceries-plugin` folder as `models.py` (`kb(workbench.action.files.saveAs)`): + ```python + from typing import Optional + from pydantic import BaseModel + + class ItemPayload(BaseModel): + item_id: Optional[int] + item_name: str + quantity: int + ``` + +[Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance), the default language server for Python in VS Code, supports type hinting features that can be helpful for working with Pydantic models and FastAPI. This is because Pylance is built on top of [Pyright](https://github.com/microsoft/pyright), which is a static type checker for Python that can detect type errors in your code to prevent bugs and improve code quality. + +The 3 steps below are optional, but given FastAPI leverages type hints extensively to improve code readability and validation, we can take advantage of Pylance's type checking features to catch errors early on: + +1. Open the Settings UI page (`kb(workbench.action.openSettings)`) +2. Search for "python type checking mode" and set it to "basic" to enable basic type checking. This will enable Pylance to show diagnostics and warnings to catch simple type-related errors. Alternatively, you can set it to "strict" to enable more advanced [type checking rules](https://microsoft.github.io/pyright/#/configuration?id=diagnostic-rule-defaults). + ![Python Analysis Type Checking Mode options (off, basic and strict) in settings UI page.](images/fastapi-tutorial/type_checking_mode_setting.png) +3. Next, search for "Python inlay type hints", and enable inlay hints for Variable Types and Function Return Types: + ![Two Python Analysis Type Hints settings being enabled in the settings UI page: for Function Return Types and for Variable Types.](images/fastapi-tutorial/function_and_variable_return_type_hint_settings.png) + +## Create Routes +Now we need a place to store the grocery list items. For simplicity, let’s start with an empty dictionary. +1. First, let's import all the packages we will need for the sample. Open the `main.py` file and replace the first import line with the following ones: + ```python + from fastapi import FastAPI, HTTPException + + from models import ItemPayload + ``` +2. Now add the following line right below `app = FastAPI()`: + ```python + grocery_list: dict[int, ItemPayload] = {} + ``` + + This creates a new empty dictionary that will receive keys of type `int` (which will be item IDs) and values of the `ItemPayload` type. + +We'll now define routes in our FastAPI application. In the context of web applications, routes are like pathways that map specific URLs to the code that handles them. These routes serve as the entry points for the different functionality within our application. When a client, such as a web browser or another program, sends a request to our application with a particular URL, FastAPI routes that request to the appropriate function (also known as route handler or view function) based on the URL, and that function processes the request and generates a response. + +Let's proceed with defining routes that will allow us to add and retrieve individual items, as well as list all items in the grocery list. + +3. Add the following route at the end of the `main.py` file: + ```python + # Route to add a item + @app.post("/items/{item_name}/{quantity}") + def add_item(item_name: str, quantity: int): + if quantity <= 0: + raise HTTPException(status_code=400, detail="Quantity must be greater than 0.") + # if item already exists, we'll just add the quantity. + # get all item names + items_ids = {item.item_name: item.item_id if item.item_id is not None else 0 for item in grocery_list.values()} + if item_name in items_ids.keys(): + # get index of item_name in item_ids, which is the item_id + item_id = items_ids[item_name] + grocery_list[item_id].quantity += quantity + # otherwise, create a new item + else: + # generate an id for the item based on the highest ID in the grocery_list + item_id = max(grocery_list.keys()) + 1 if grocery_list else 0 + grocery_list[item_id] = ItemPayload( + item_id=item_id, item_name=item_name, quantity=quantity + ) + + return {"item": grocery_list[item_id]} + ``` + + If you have enabled type hints in the previous section, you might notice Pylance adds inlay hints with the function return type, as well as the types for `item_ids` and `item_id`. You can optionally double-click on each suggestion to insert them into the code: + ![Inlay function return and variable type hints being displayed by Pylance throughout the sample code.](images/fastapi-tutorial/pylance_inlay_hints.png) + +Now let's see if this route is working as expected. The fastest way to do so is to leverage both VS Code's debugger as well as FastAPI's `/docs` endpoint, which provides information about all the available API routes and allows you to interact with the API to explore their parameters and responses. This documentation is generated dynamically based on the metadata and type hints defined in the FastAPI application. + +4. Add a breakpoint next to the if `quantity <= 0` statement, by clicking on the left margin of the line number (or `kb(editor.debug.action.toggleBreakpoint)`). This will make the debugger stop prior to the execution of that line, allowing us to inspect the code line by line. + ![Breakpoint set next to the first line in the add_item function.](images/fastapi-tutorial/debugger_breakpoint.png) + +5. Start the debugger (`kb(workbench.action.debug.start)`), and then navigate to http://127.0.0.1:8000/docs in the browser. + + You'll see a Swagger interface with the two endpoints available in the app: `/items` and root (`/`). + ![Swagger UI displaying two endpoints: /items and /](images/fastapi-tutorial/fastapi_first_swagger_page.png) + +6. Click on the down arrow next to the `/items` route to expand it, and click on the "Try it out" button that appears on the right side. + ![Try it out button displayed next to the /items route in the Swagger UI.](images/fastapi-tutorial/fastapi_tryitout_button.png) + +7. Add a grocery list item by passing a string to the "item" field, and a number to "quantity". For example, you could provide apple as the `item_name` and 2 as the `quantity`. + +8. Click on the "Execute" button. + ![Execute button displayed below the /items route.](images/fastapi-tutorial/fastapi_execute_button.png) + +9. Open VS Code again and notice the debugger has stopped at the breakpoint we set earlier. + ![Debugger stopped at the breakpoint set in the add_item function.](images/fastapi-tutorial/fastapi_breakpoint_hit.png) + +On the left side, you will see all local and global variables defined at this point in the Variables window, under the **Run and Debug** view. In our example, we can see `item_name` set to apple and `quantity` set to 2 under the locals variable view, as well as an empty `grocery_list` dictionary under the globals variable view. + +![Variables window displayed in the Run and Debug view, with the item and grocery_list variables highlighted.](images/fastapi-tutorial/fastapi_debugger_variables.png) + +Now let’s leverage VS Code's Debug Console to do some exploration. + +10. Select the `quantity <= 0` statement, right-click on the editor and select **Evaluate in Debug Console**: + ![Evaluate in Debug Console option displayed in the context menu when right-clicking on a line of code.](images/fastapi-tutorial/fastapi_evaluate_debug_console.png) + + This will open the Debug Console and run the selected expression. As expected in our example, the expression will be evaluated to `False`. + +The Debug Console can be a powerful tool to quickly test expressions and better understand the state of your code at the time of a breakpoint. You can also use it to run arbitrary code, such as calling functions or printing variables. You can learn more about Python debugging in VS Code in the [Python tutorial](python-tutorial.md#configure-and-run-the-debugger). + +You can now continue the execution of the code by clicking on the "Continue" button in the debug toolbar, or by pressing `F5` (`kb(workbench.action.debug.continue)`). + +Finally, let's add the remaining routes for the application so we can list all items or specific items, as well as remove them from our grocery list. You can leave the debugger running as it will automatically reload the application when you save the changes you'll make in the next step. + +11. Replace the content in `main.py` with the code below: + ```python + from fastapi import FastAPI, HTTPException + + from models import ItemPayload + + app = FastAPI() + + grocery_list: dict[int, ItemPayload] = {} + + # Route to add an item + @app.post("/items/{item_name}/{quantity}") + def add_item(item_name: str, quantity: int) -> dict[str, ItemPayload]: + if quantity <= 0: + raise HTTPException(status_code=400, detail="Quantity must be greater than 0.") + # if item already exists, we'll just add the quantity. + # get all item names + items_ids: dict[str, int] = { + item.item_name: item.item_id if item.item_id is not None else 0 + for item in grocery_list.values() + } + if item_name in items_ids.keys(): + # get index of item_name in item_ids, which is the item_id + item_id: int = items_ids[item_name] + grocery_list[item_id].quantity += quantity + # otherwise, create a new item + else: + # generate an ID for the item based on the highest ID in the grocery_list + item_id: int = max(grocery_list.keys()) + 1 if grocery_list else 0 + grocery_list[item_id] = ItemPayload( + item_id=item_id, item_name=item_name, quantity=quantity + ) + + return {"item": grocery_list[item_id]} + + + # Route to list a specific item by id + @app.get("/items/{item_id}") + def list_item(item_id: int) -> dict[str, ItemPayload]: + if item_id not in grocery_list: + raise HTTPException(status_code=404, detail="Item not found.") + return {"item": grocery_list[item_id]} + + + # Route to list all items + @app.get("/items") + def list_items() -> dict[str, dict[int, ItemPayload]]: + return {"items": grocery_list} + + + # Route to delete a specific item by id + @app.delete("/items/{item_id}") + def delete_item(item_id: int) -> dict[str, str]: + if item_id not in grocery_list: + raise HTTPException(status_code=404, detail="Item not found.") + del grocery_list[item_id] + return {"result": "Item deleted."} + + + # Route to remove some quantity of a specific item by id + @app.delete("/items/{item_id}/{quantity}") + def remove_quantity(item_id: int, quantity: int) -> dict[str, str]: + if item_id not in grocery_list: + raise HTTPException(status_code=404, detail="Item not found.") + # if quantity to be removed is higher or equal to item's quantity, delete the item + if grocery_list[item_id].quantity <= quantity: + del grocery_list[item_id] + return {"result": "Item deleted."} + else: + grocery_list[item_id].quantity -= quantity + return {"result": f"{quantity} items removed."} + + ``` + +12. Save the file (`kb(workbench.action.files.save)`). The application should automatically reload. + +You can now open the `/docs` page again and test the new routes, leveraging the debugger and the debug console to better understand the code execution. Once you're done, you can stop the debugger (`kb(workbench.action.debug.stop)`). You may also remove the breakpoint we added on step 4 by clicking on it. + +Congratulations! You now have a working FastAPI application with routes to add, list, and delete items from a grocery list. + +## Set up the data storage +At this point, you already have a working version of the application with the base functionality. This section will guide you through setting up data storage for persistence, but you can choose to skip it if you're happy with what you've learned already. + +So far we are storing the data in a dictionary, which is not ideal because all of the data will be lost when the application is restarted. + +To persist the data, we will use [Redis](https://redis.io/), which is an open source in-memory data structure store. Due to its speed and versatility, Redis is commonly used as a data storage system in a wide range of applications, including web applications, real-time analytics systems, caching layers, this tutorial, and more. + +If you are already working on **GitHub Codespaces** with our existing template, you can skip directly to the ["Replace the database"](#replace-the-database) section. + +If you are on Windows, you can work with Redis by setting up either a [Docker container](https://www.docker.com/products/docker-desktop/), a [GitHub Codespace](https://github.com/features/codespaces) or through [WSL](https://learn.microsoft.com/en-us/windows/wsl/) (Windows Subsystem for Linux). In this tutorial we will use a Docker container, but you can refer to the [section above](#github-codespaces) for instructions on how to set up a GitHub Codespace. + +Otherwise, if you are on a Linux or a macOS machine, you can install Redis by following the [instructions on their website](https://redis.io/docs/getting-started/), and then skip to the ["Replace the database"](#replace-the-database) section. + +### Setting up a Docker Container on Windows +The [VS Code Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) offers a streamlined approach to consolidate your project, its dependencies and all necessary tools into one tidy container, creating a full-featured development environment. The extension enables you to open your project inside (or mounted into) the container in VS Code, leveraging its complete range of feature set. + +For the steps below, make sure you have the following requirements installed on your machine: + +#### Requirements +- [Docker for Windows](https://www.docker.com/) +- [VS Code Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) + +#### Create the Dev container configuration +1. Open the Command Palette and run the "Dev Containers: Add Dev Container Configuration Files…" +2. Select "Python 3": + + Python 3 option selected in the Dev Containers configuration files list. +3. Select the default version. + +We can optionally install [Features](https://github.com/devcontainers/features) to be included in the container. For this tutorial, we will install [Redis Server](https://github.com/itsmechlark/features/tree/main/src/redis-server), which is a community contributed Feature that installs and adds the proper dev container set up for Redis. + +4. Select "Redis Server" as an additional feature to be installed, press "OK", and then select "Keep Defaults". + Redis Server option selected in the Dev Containers configuration files list. + +This will create a `.devcontainer` folder in your workspace, with a `devcontainer.json` file. Let's make some edits to this file so the container setup will include steps such as installing the VS Code extensions we'll need as well as the project dependencies. + +5. Open the `devcontainer.json` file. +6. Add a "," after the `"features" : { ... }` entry, so we can add more settings to the file. + +Next, we will add the necessary dependency installation commands to the `postCreateCommand` property in the `devcontainer.json` file, so our application will be ready to run once the container is set up. + +7. Locate the content below and remove the comment (`//`) from that line, so the dependencies can be installed once the container is created: + ``` + "postCreateCommand": "pip3 install --user -r requirements.txt", + ``` + You can learn more about the `postCreateCommand` and more in the [documentation](https://containers.dev/implementors/json_reference/#lifecycle-scripts). + + Now we will use the `customizations` property to add the VS Code extensions we will want to be installed in the container. + +8. Add the following setting to `devcontainer.json`: + ``` + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "pip3 install --user -r requirements.txt", + + // Configure tool-specific properties. + "customizations": { + "vscode": { + "extensions": [ + "ms-python.python", //Python extension ID + "ms-python.vscode-pylance" //Pylance extension ID + ] + } + } + ``` +9. Save the file. + +10. Select the "Reopen in Container" button from the notification that will show up on the bottom right corner, or run the **Dev Containers: Reopen in Container** command from the Command Palette. + + > Note: it may take several minutes to build the container, depending on internet speed and machine performance. + + You can learn more about dev containers configuration in the [documentation](https://code.visualstudio.com/docs/devcontainers/containers#_create-a-devcontainerjson-file). + +Once it's done, you will have a fully configured Linux-based workspace with Python 3 and Redis Server installed. + +Once the container is set up, you will notice an indicator on the bottom left corner of VS Code: + +![Dev Containers indicator displayed on the bottom left corner of VS Code.](images/fastapi-tutorial/devcontainer_indicator.png) + +> Note: Double check that the Python and Pylance extensions have been successfully installed in the container by opening the Extensions view (`kb(workbench.view.extensions)`) and searching for them. If not, you can install them by clicking on the "Install in Dev Container" button. + +You should also see the selected Python interpreter information on the status bar at the bottom right corner, matching the version specified in the `devcontainer.json` file: + +Python interpreter selection + +> Note: If you don't see the Python interpreter information on the status bar, you can click on the Python interpreter indicator (or run the **Python: Select Interpreter** command from the Command Palette) and manually select the Python interpreter in the container. + +We are now ready to move on to the next section, where we will replace the data storage. + +## Replace the database + +We have a dictionary that stores the grocery list items, but we want to replace it with a Redis database. In this tutorial, we will use Redis hashes to store our data, which is a data structure that can store multiple key-value pairs. + +Unlike a traditional database where you can retrieve an item without knowing its ID, you need to know the Redis hash key in order to retrieve a value from it. In this tutorial, we will create a hash called `item_name_to_id` that will allow us to retrieve items by name, and map them to their IDs. In addition, we'll create other hashes that will allow us to retrieve items by ID, mapping them to their names and quantities. Each item hash will be named `item_id:{item_id}` and will have two fields: `item_name` and `quantity`. + + +First, let's start by replacing the dictionary with a Redis client object that connects to a Redis server. + +1. In the `main.py` file, replace the `grocery_list: dict[int, ItemPayload] = {}` in the beginning of the file with the lines below: + ```python + redis_client = redis.StrictRedis(host='0.0.0.0', port=6379, db=0, decode_responses=True) + ``` +Pylance will display an error message because Redis hasn’t been imported yet. + +2. Click on "redis" in the editor, and select the light bulb that shows up (or `kb(editor.action.quickFix)`). Then select **Add 'import redis'**. + ![Light bulb displayed next to the Redis variable, with the option to add the import statement.](images/fastapi-tutorial/fastapi_add_redis_quickfix.png) + + > **Tip**: You can set up Pylance to automatically add imports by looking for the "Auto Import Completions" setting in the Settings UI page (`kb(workbench.action.openSettings)`) and enabling it. + +We now have a Redis client object that connects to a Redis server running on the local host (`host="0.0.0.0"`) and listening on port 6379 (`port=6379`). The `db` parameter specifies the Redis database to use. Redis supports multiple databases, and in this code we’re setting to use database 0, which is the default database. We're also passing `decode_responses=True` for the responses to be decoded as strings (instead of bytes). + +Let's do some more replacements in the first route `add_item`. Instead of looking into all the keys from the dictionary to find the item name that has been provided, we can fetch that information directly from a Redis hash. + +We'll assume that the `item_name_to_id` hash already exists, mapping item names to their IDs (don't worry, we'll add this code shortly!). We can then get the ID of the item name we’re receiving in the request by invoking the `hget` method from Redis, which will return the item ID if the requested name already exists in the hash, or `None` if it doesn't. + +3. Delete the line with the content below: + ```python + items_ids = {item.item_name: item.item_id if item.item_id is not None else 0 for item in grocery_list.values()} + ``` + And replace it with: + ```python + item_id = redis_client.hget("item_name_to_id", item_name) + ``` + +Note that Pylance raises a problem with this change. This is because the `hget` method returns either `str`, or `None` (if the item doesn’t exist). However, the lines below the code that we haven’t replaced yet expect `item_id` to be of type `int`. Let’s address it by renaming the `item_id` symbol. + +4. Rename `item_id` to `item_id_str`. + +5. If you have inlay hints enabled, Pylance should show a variable type hint next to `item_id_str`. You can optionally double click to accept it: + + ![Variable type hint displayed next to the item_id_str variable.](images/fastapi-tutorial/pylance_redis_typehint.png) + +6. If the item doesn't exist, then `item_id_str` will be None. So now we can delete the line with the following content: + ```python + if item_name in items_ids.keys(): + ``` + And replace it with: + ```python + if item_id_str is not None: + ``` + +Now that we have the item ID as a string, we need to convert it to an `int` and update the quantity for the item. Currently, our Redis hash only maps item names to their IDs. To also map item IDs to their names and quantities, we will create a separate Redis hash for each item, using `"item_id:{item_id}"` as our hash name to make retrieval by ID easier. We'll also add `item_name` and `quantity` fields for each of these hashes. + +7. Delete the code within the `if` block: + ```python + item_id: int = items_ids[item_name] + grocery_list[item_id].quantity += quantity + ``` + And add the following, to convert the `item_id` to an `int`, and then to increment the quantity of the item by calling the `hincrby` method from Redis. This method will increment the value of the `"quantity"` field by the given amount in the request (`quantity`): + + ```python + item_id = int(item_id_str) + redis_client.hincrby(f"item_id:{item_id}", "quantity", quantity) + ``` + + +We now only need to replace the code for when the item does not exist, i.e. `item_id_str` is `None`. In this case, we will generate a new `item_id`, create a new Redis hash for the item, and then add the provided item name and quantity. +To generate a new `item_id`, let’s use the `incr` method from Redis, passing a new hash called `"item_ids"`. This hash will be used to store the last generated ID, so we can increment it each time we create a new item, ensuring that they all have a unique ID. + +8. Delete the line with the following content: + ```python + item_id: int = max(grocery_list.keys()) + 1 if grocery_list else 0 + ``` + And add the following: + ```python + item_id: int = redis_client.incr("item_ids") + ``` + + When this `incr` call is run for the first time with the `item_ids` key, Redis will create the key and map it to the value `1`. Then, each subsequent time it's run, it will increment the stored value by 1. + +Now we will add the item to the Redis hash, using the `hset` method and by providing a mapping the fields (`item_id`, `quantity` and `name`), and the values (the item’s newly created id, and its provided name and quantity). + +9. Delete the line with the following content: + ```python + grocery_list[item_id] = ItemPayload( + item_id=item_id, item_name=item_name, quantity=quantity + ) + ``` + And replace it with the following: + ```python + redis_client.hset( + f"item_id:{item_id}", + mapping={ + "item_id": item_id, + "item_name": item_name, + "quantity": quantity, + }) + ``` + +Now we only need to map the newly created ID to the item name by setting the hash we referenced in the beginning, `item_name_to_id`. + +10. Add this line to the end of the route, inside the `else` block: + ```python + redis_client.hset("item_name_to_id", item_name, item_id) + ``` + +11. Delete the line with the following content: + ```python + return {"item": grocery_list[item_id]} + ``` + And replace it with: + ```python + return {"item": ItemPayload(item_id=item_id, item_name=item_name, quantity=quantity)} + ``` + +12. If you would like, you can try to do a similar replacement for the other routes. Otherwise, you can just replace the entire content of the file with the lines below: + ```python + import redis + from fastapi import FastAPI, HTTPException + + from models import ItemPayload + + app = FastAPI() + + redis_client = redis.StrictRedis(host="0.0.0.0", port=6379, db=0, decode_responses=True) + + # Route to add an item + @app.post("/items/{item_name}/{quantity}") + def add_item(item_name: str, quantity: int) -> dict[str, ItemPayload]: + if quantity <= 0: + raise HTTPException(status_code=400, detail="Quantity must be greater than 0.") + + # Check if item already exists + item_id_str: str | None = redis_client.hget("item_name_to_id", item_name) + + if item_id_str is not None: + item_id = int(item_id_str) + redis_client.hincrby(f"item_id:{item_id}", "quantity", quantity) + else: + # Generate an id for the item + item_id: int = redis_client.incr("item_ids") + redis_client.hset( + f"item_id:{item_id}", + mapping={ + "item_id": item_id, + "item_name": item_name, + "quantity": quantity, + }, + ) + # Create a set so we can search by name too + redis_client.hset("item_name_to_id", item_name, item_id) + + return { + "item": ItemPayload(item_id=item_id, item_name=item_name, quantity=quantity) + } + + + # Route to list a specific item by id but using Redis + @app.get("/items/{item_id}") + def list_item(item_id: int) -> dict[str, dict[str, str]]: + if not redis_client.hexists(f"item_id:{item_id}", "item_id"): + raise HTTPException(status_code=404, detail="Item not found.") + else: + return {"item": redis_client.hgetall(f"item_id:{item_id}")} + + + @app.get("/items") + def list_items() -> dict[str, list[ItemPayload]]: + items: list[ItemPayload] = [] + stored_items: dict[str, str] = redis_client.hgetall("item_name_to_id") + + for name, id_str in stored_items.items(): + item_id: int = int(id_str) + + item_name_str: str | None = redis_client.hget(f"item_id:{item_id}", "item_name") + if item_name_str is not None: + item_name: str = item_name_str + else: + continue # skip this item if it has no name + + item_quantity_str: str | None = redis_client.hget( + f"item_id:{item_id}", "quantity" + ) + if item_quantity_str is not None: + item_quantity: int = int(item_quantity_str) + else: + item_quantity = 0 + + items.append( + ItemPayload(item_id=item_id, item_name=item_name, quantity=item_quantity) + ) + + return {"items": items} + + + # Route to delete a specific item by id but using Redis + @app.delete("/items/{item_id}") + def delete_item(item_id: int) -> dict[str, str]: + if not redis_client.hexists(f"item_id:{item_id}", "item_id"): + raise HTTPException(status_code=404, detail="Item not found.") + else: + item_name: str | None = redis_client.hget(f"item_id:{item_id}", "item_name") + redis_client.hdel("item_name_to_id", f"{item_name}") + redis_client.delete(f"item_id:{item_id}") + return {"result": "Item deleted."} + + + # Route to remove some quantity of a specific item by id but using Redis + @app.delete("/items/{item_id}/{quantity}") + def remove_quantity(item_id: int, quantity: int) -> dict[str, str]: + if not redis_client.hexists(f"item_id:{item_id}", "item_id"): + raise HTTPException(status_code=404, detail="Item not found.") + + item_quantity: str | None = redis_client.hget(f"item_id:{item_id}", "quantity") + + # if quantity to be removed is higher or equal to item's quantity, delete the item + if item_quantity is None: + existing_quantity: int = 0 + else: + existing_quantity: int = int(item_quantity) + if existing_quantity <= quantity: + item_name: str | None = redis_client.hget(f"item_id:{item_id}", "item_name") + redis_client.hdel("item_name_to_id", f"{item_name}") + redis_client.delete(f"item_id:{item_id}") + return {"result": "Item deleted."} + else: + redis_client.hincrby(f"item_id:{item_id}", "quantity", -quantity) + return {"result": f"{quantity} items removed."} + + ``` + +13. Re-run the debugger to test this application by interacting with the `/docs` route. You can stop the debugger once you're done. + +Congrats! You now have a working FastAPI application with routes to add, list, and delete items from a grocery list, and the data being persisted in a Redis database. + +## Optional: Set up database deletion +Because the data will be now be persisted by Redis, you may want to create a script that will allow you to erase all testing data. For that you can create a new file called `flushdb.py` with the following content: +```python +import redis + +redis_client = redis.StrictRedis(host='0.0.0.0', port=6379, db=0, decode_responses=True) +redis_client.flushdb() + +``` +Then when you want to reset the database, you can open the flushdb.py file in VS Code and click on the run button on the top-right corner of the editor, or run the **Python: Run Python File in Terminal** command from the Command Palette. + +Note that this should be done with caution because it will delete all the keys in the current database, which could lead to data loss if done in production. + +## Optional: Create a ChatGPT plugin using GitHub Codespaces +With GitHub Codespaces, we can host our application for testing purposes to use with [ChatGPT Plugins](https://platform.openai.com/docs/plugins/introduction), which are tools that enable [ChatGPT](https://chat.openai.com/) to interact with existing APIs to enhance ChatGPT’s abilities, allowing it to perform a wide range of actions. ChatGPT Plugins are not currently publicly available, but you can join their [wait list](https://openai.com/waitlist/plugins) to get access. Once you do, you can follow along the live stream recording below to create your own grocery list plugin for ChatGPT: + + +> Note: All personal GitHub.com accounts have a monthly quota of free use of GitHub Codespaces included in the Free or Pro plan. For more information, see "[About billing for GitHub Codespaces](https://docs.github.com/en/billing/managing-billing-for-github-codespaces/about-billing-for-github-codespaces)". + +## Next Steps + +Thank you for following along this tutorial! We hope you learned something new about FastAPI and how to use it with VS Code. + +The completed code project from this tutorial can be found on GitHub: [python-sample-vscode-fastapi-tutorial](https://github.com/microsoft/python-sample-vscode-fastapi-tutorial). + +Learn more about FastAPI on the [official documentation](https://fastapi.tiangolo.com/). + +To try the app on a production website, check out the tutorial [Deploy Python apps to Azure App Service using Docker Containers](https://learn.microsoft.com/azure/developer/python/tutorial-deploy-containers-01). + +You may also want to review the following articles in the VS Code docs that are relevant to Python: + +- [Editing Python code](/docs/python/editing.md) +- [Managing Python environments](/docs/python/environments.md) +- [Debugging Python](/docs/python/debugging.md) +- [Testing](/docs/python/testing.md) diff --git a/docs/python/tutorial-flask.md b/docs/python/tutorial-flask.md index 5a2d74b1a2..ddec31bc8f 100644 --- a/docs/python/tutorial-flask.md +++ b/docs/python/tutorial-flask.md @@ -1,5 +1,5 @@ --- -Order: 10 +Order: 11 Area: python TOCTitle: Flask Tutorial ContentId: 593d2dd6-20f0-4ad3-8ecd-067cc47ee217 diff --git a/docs/sourcecontrol/github.md b/docs/sourcecontrol/github.md index 5f713ba80b..5ac93d2ee7 100644 --- a/docs/sourcecontrol/github.md +++ b/docs/sourcecontrol/github.md @@ -253,4 +253,4 @@ In VS Code, you can enhance your coding with artificial intelligence (AI), such [![GitHub Copilot extension in the VS Code Marketplace](images/github/copilot-extension.png)](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) -You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/artificial-intelligence.md). \ No newline at end of file +You can learn more about how to get started with Copilot in the [Copilot documentation](/docs/editor/github-copilot.md). \ No newline at end of file diff --git a/release-notes/v1_75.md b/release-notes/v1_75.md index bbbc0d2cc2..861579baff 100644 --- a/release-notes/v1_75.md +++ b/release-notes/v1_75.md @@ -589,7 +589,7 @@ GitHub Copilot is now generally available for businesses, with features like lic To get started, you can sign up for a free trial on the [GitHub Copilot website](https://copilot.github.com). -We've also added a new [AI Tools in VS Code](https://code.visualstudio.com/docs/editor/artificial-intelligence) topic to the VS Code documentation that will help you get started with Copilot. +We've also added a new [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/editor/github-copilot) topic to the VS Code documentation that will help you get started with Copilot. ## Remote Development diff --git a/release-notes/v1_77.md b/release-notes/v1_77.md index 7034a9cd4b..f0b3d53d86 100644 --- a/release-notes/v1_77.md +++ b/release-notes/v1_77.md @@ -239,7 +239,7 @@ You can ask Copilot to look for bugs, explain tricky code, create tests, and eve ![Copilot chat example asking how to change VS Code colors](images/1_77/slash-commands-example.png) -You can learn more about the VS Code team's experience and future with Copilot in the [VS Code and GitHub Copilot](https://code.visualstudio.com/blogs/2023/03/30/vscode-copilot) blog post. You can also read the [AI Tools in VS Code](https://code.visualstudio.com/docs/editor/artificial-intelligence) article for more details about using Copilot in VS Code. +You can learn more about the VS Code team's experience and future with Copilot in the [VS Code and GitHub Copilot](https://code.visualstudio.com/blogs/2023/03/30/vscode-copilot) blog post. You can also read the [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/editor/github-copilot) article for more details about using Copilot in VS Code. ## Preview features