You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/python/debugging.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,9 +48,9 @@ To generate a `launch.json` file with Python configurations, do the following st
48
48
49
49
1. A configuration menu will open from the Command Palette allowing you to choose the type of debug configuration you want to use for our Python project file. If you want to debug a single Python script, select **Python File** in the **Select a debug configuration** menu that appears.
50
50
51
-

51
+

52
52
53
-
> **Note**: Starting a debugging session through the Debug Panel, `kbstyle(F5)` or **Run > Start Debugging** when no configuration exists will also bring up the debug configuration menu, but will not create a launch.json file.
53
+
> **Note**: Starting a debugging session through the Debug Panel, `kbstyle(F5)`, or **Run > Start Debugging** when no configuration exists will also bring up the debug configuration menu, but will not create a `launch.json` file.
54
54
55
55
1. The Python Debugger extension then creates and opens a `launch.json` file that contains a pre-defined configuration based on what you previously selected, in this case, **Python File**. You can modify configurations (to add arguments, for example), and also add custom configurations.
Copy file name to clipboardExpand all lines: docs/python/python-tutorial.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -158,9 +158,9 @@ There are three other ways you can run Python code within VS Code:
158
158
159
159
2. Select one or more lines, then press `kbstyle(Shift+Enter)` or right-click and select **Run Python > Run Selection/Line in Python Terminal**. Alternatively, you can activate Smart Send using `kbstyle(Shift+Enter)` without a selection and the Python extension will send the smallest runnable block of code near where your cursor is placed to the terminal. This command is convenient for testing just a part of a file.
160
160
161
-
> **Note**: If you prefer to send code at the particular line your cursor is placed, you can turn off Smart Send by setting `python.REPL.enableREPLSmartSend : "false"` in your **User** settings.
161
+
> **Note**: If you prefer to send code at the particular line your cursor is placed, you can turn off Smart Send by setting `python.REPL.enableREPLSmartSend : "false"` in your **User** settings.
162
162
163
-
1. From the Command Palette (`kb(workbench.action.showCommands)`), select the **Python: Start Terminal REPL** command to open a REPL terminal (notated by `>>>`) for the currently selected Python interpreter. In the REPL, you can then enter and run lines of code one at a time.
163
+
3. From the Command Palette (`kb(workbench.action.showCommands)`), select the **Python: Start Terminal REPL** command to open a REPL terminal (notated by `>>>`) for the currently selected Python interpreter. In the REPL, you can then enter and run lines of code one at a time.
164
164
165
165
Congrats, you just ran your first Python code in Visual Studio Code!
Copy file name to clipboardExpand all lines: docs/python/tutorial-fastapi.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -185,9 +185,9 @@ Now we need a place to store the grocery list items. For simplicity, let's start
185
185
186
186
This creates a new empty dictionary that receives keys of type `int` (as item IDs) and values of the `ItemPayload` type.
187
187
188
-
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.
188
+
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.
189
189
190
-
Let's proceed with defining routes to add and retrieve individual items, as well as return all items in the grocery list.
190
+
Let's proceed with defining routes to add and retrieve individual items, as well as return all items in the grocery list.
191
191
192
192
3. Add the following route at the end of the `main.py` file:
193
193
@@ -219,7 +219,7 @@ Let's proceed with defining routes to add and retrieve individual items, as well
219
219
220
220

221
221
222
-
Now let's check if this route is working as expected. The fastest way to do so is to use both VS Code's debugger as well as FastAPI's `/docs` endpoint, which provides information about all the available API routes and lets you 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.
222
+
Now let's check if this route is working as expected. The fastest way to do so is to use both VS Code's debugger as well as FastAPI's `/docs` endpoint, which provides information about all the available API routes and lets you 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.
223
223
224
224
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)`). The debugger will stop prior to the execution of that line, so you can inspect the code line by line.
225
225
@@ -249,19 +249,19 @@ Now let's check if this route is working as expected. The fastest way to do so i
249
249
250
250

251
251
252
-
Now let's use VS Code's Debug Console to do some exploration.
252
+
Now let's use VS Code's Debug Console to do some exploration.
253
253
254
254
10. Select the `quantity <= 0` statement, right-click on the editor and select **Evaluate in Debug Console**:
255
255
256
256

257
257
258
258
This opens the Debug Console and runs the selected expression. As expected in our example, the expression evaluates to `False`.
259
259
260
-
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](/docs/python/python-tutorial.md#configure-and-run-the-debugger).
260
+
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](/docs/python/python-tutorial.md#configure-and-run-the-debugger).
261
261
262
-
You can now continue the execution of the code by selecting **Continue** in the Debug view tool bar, or by pressing `kb(workbench.action.debug.continue)`.
262
+
You can now continue the execution of the code by selecting **Continue** in the Debug view tool bar, or by pressing `kb(workbench.action.debug.continue)`.
263
263
264
-
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 make in the next step.
264
+
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 make in the next step.
265
265
266
266
11. Replace the content in `main.py` with the code below:
267
267
@@ -379,19 +379,19 @@ For the steps below, make sure you have the following requirements installed on
379
379
380
380
3. Select the default version.
381
381
382
-
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), a community contributed Feature that installs and adds the proper dev container setup for Redis.
383
-
384
382
4. Select **Redis Server** as an additional feature to be installed, press **OK**, and then select **Keep Defaults**.
385
383
384
+
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), a community contributed Feature that installs and adds the proper dev container setup for Redis.
385
+
386
386

387
387
388
-
This creates a `.devcontainer` folder in your workspace, with a `devcontainer.json` file. Let's make some edits to this file so the container setup includes steps such as installing the VS Code extensions we need as well as the project dependencies.
388
+
This creates a `.devcontainer` folder in your workspace, with a `devcontainer.json` file. Let's make some edits to this file so the container setup includes steps such as installing the VS Code extensions we need as well as the project dependencies.
389
389
390
390
5. Open the `devcontainer.json` file.
391
391
392
392
6. Add a "," after the `"features" : { ... }` entry, so we can add more settings to the file.
393
393
394
-
Next, we will add the necessary dependency installation commands to the `postCreateCommand` property in the `devcontainer.json` file, so our application is ready to run once the container is set up.
394
+
Next, we will add the necessary dependency installation commands to the `postCreateCommand` property in the `devcontainer.json` file, so our application is ready to run once the container is set up.
395
395
396
396
7. Locate the content below and remove the comment (`//`) from that line, so the dependencies can be installed once the container is created:
397
397
@@ -458,19 +458,19 @@ First, let's start by replacing the dictionary with a Redis client object that c
Pylance will display an error message because Redis hasn't been imported yet.
461
+
Pylance will display an error message because Redis hasn't been imported yet.
462
462
463
463
2. Put the cursor on "redis" in the editor, and click on the displayed light bulb (or `kb(editor.action.quickFix)`). Then select **Add 'import redis'**.
464
464
465
465

466
466
467
467
>**Tip**: You can set up Pylance to automatically add imports by looking for the **Auto Import Completions** setting in the Settings editor (`kb(workbench.action.openSettings)`) and enabling it.
468
468
469
-
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 going 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).
469
+
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 going 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).
470
470
471
-
Let's do some more replacements in the first route `add_item`. Instead of looking at 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.
471
+
Let's do some more replacements in the first route `add_item`. Instead of looking at 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.
472
472
473
-
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.
473
+
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.
474
474
475
475
3. Delete the line with the content below:
476
476
@@ -484,7 +484,7 @@ We'll assume that the `item_name_to_id` hash already exists, mapping item names
Notice 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 this warning by renaming the `item_id` symbol.
487
+
Notice 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 this warning by renaming the `item_id` symbol.
488
488
489
489
4. Rename `item_id` to `item_id_str`.
490
490
@@ -504,7 +504,7 @@ Notice that Pylance raises a problem with this change. This is because the `hget
504
504
if item_id_str is not None:
505
505
```
506
506
507
-
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.
507
+
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.
508
508
509
509
7. Delete the code within the `if` block:
510
510
@@ -520,9 +520,9 @@ Now that we have the item ID as a string, we need to convert it to an `int` and
We now only need to replace the code for when the item does not exist, when `item_id_str` is `None`. In this case, we generate a new `item_id`, create a new Redis hash for the item, and then add the provided item name and quantity.
523
+
We now only need to replace the code for when the item does not exist, when `item_id_str` is `None`. In this case, we generate a new `item_id`, create a new Redis hash for the item, and then add the provided item name and quantity.
524
524
525
-
To generate a new `item_id`, let's use the `incr` method from Redis, passing a new hash called `"item_ids"`. This hash is 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.
525
+
To generate a new `item_id`, let's use the `incr` method from Redis, passing a new hash called `"item_ids"`. This hash is 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.
526
526
527
527
8. Delete the line with the following content:
528
528
@@ -538,7 +538,7 @@ To generate a new `item_id`, let's use the `incr` method from Redis, passing a n
538
538
539
539
When this `incr` call is run for the first time with the `item_ids` key, Redis creates the key and maps it to the value `1`. Then, each subsequent time it's run, it increments the stored value by 1.
540
540
541
-
Now we will add the item to the Redis hash, using the `hset` method and by providing a mapping for the fields (`item_id`, `item_name`, and `quantity`), and the values (the item's newly created ID, and its provided name and quantity).
541
+
Now we will add the item to the Redis hash, using the `hset` method and by providing a mapping for the fields (`item_id`, `item_name`, and `quantity`), and the values (the item's newly created ID, and its provided name and quantity).
542
542
543
543
9. Delete the line with the following content:
544
544
@@ -560,7 +560,7 @@ Now we will add the item to the Redis hash, using the `hset` method and by provi
560
560
})
561
561
```
562
562
563
-
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`.
563
+
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`.
564
564
565
565
10. Add this line to the end of the route, inside the `else` block:
0 commit comments