Visual Studio Code for Python Developers Transcripts
Chapter: Editing Python Code with VS Code
Lecture: Code Snippets
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
All right, so let's take a look at snippets and how they can help us be more efficient when writing Python code in VS Code.
0:08
First of all, what are snippets? Well, you can kind of think of them as like a text expansion, but focused on code.
0:15
And the idea behind it is so that you don't have to type as much as you normally would,
0:20
or you don't have to keep typing common things that happen or that you use very often, like API creation functions and that sort of thing.
0:29
So that's the idea behind snippets. You can use ones that are built into VS Code.
0:34
Some of them are there, but they're mostly focused on JavaScript, TypeScript out of the box that are built in, Markdown and PHP.
0:41
But if you want one for your particular language, in this case, Python,
0:46
you can search in the marketplace for the category of snippets with the keyword of Python and you'll find some.
0:51
And in fact, I'll show you one right off the bat. Being that this project that we've been looking at throughout this course is this podcast.
0:58
API, we're using FastAPI to make this possible. So there's a lot of common ways that we can set up routes and things like that in FastAPI.
1:07
So one of the extensions I found when searching the marketplace was FastAPI snippets.
1:12
So I searched for category snippets and then specifically FastAPI.
1:16
I installed this extension and then I can see that there are various abbreviations I can type that will then generate more code for me.
1:25
However, the first thing we need to do in order to enable this is to update our settings.
1:29
If you have a fresh installed VS Code, you're likely going to have to change this because the default version of this is off.
1:37
If you go into your settings, you search for tab completions, turn it on, you'll be all set and good to go.
1:42
Install your snippets and you're ready to try them out. So the first one we're going to try out from this snippets extension is hello world HW.
1:50
So if I come in here and I add a new line, type in HW, you'll notice before I press anything else that it shows this snippet extension.
1:57
So if I come in here and I add a new line, type in HW, you'll notice before I press anything else that it shows this snippet extension.
1:57
This shows this type of indication in the IntelliSense from Visual Studio Code.
2:02
Now, if I hit tab, what that will do, if you watch closely, it's going to go by fast. It's going to generate a HelloWorld example of FastAPI.
2:09
And boom, I'm ready to go. You can see it imported FastAPI. It set up the app and set up a default route that returns a JSON message of HelloWorld,
2:19
which is great. And you can use this for all kinds of things. Now, the last thing I want to describe here is being able to create your own snippets.
2:27
So maybe I wanted to create one that redoes this logging that's specific to my particular
2:33
project here, because I'm constantly, as you can see here, writing this over and over again.
2:36
So maybe you could see I'm repeating this code over and over again on line 52, on line 57, on line 39.
2:43
So it'd be great if I could just quickly type out or create a snippet that would set it
2:48
up at least and allow me to enter in what I need, the string that I want, and then the variable.
2:53
So let's see what that's like and how I can go about doing it. So in order to do that, we're going to use the command palette, which has always been
3:00
our friend throughout all of this. So control or command shift P and let's go snippets and configure user snippets.
3:08
We can see we have some options here. There's new global snippets file, but we want one that's specific to Python. So we're going to hit enter on that.
3:14
And we can see an example here before us. Place your snippets for Python here. Here's the example.
3:20
If we want to print the console, let's undo the snippets. We can undo that one a little bit on comment that rather.
3:25
And now we have the print to console option. Not exactly what I was talking about before, but at least a starting point for us. And let's try it out.
3:33
So now that I have this log, which is the prefix log is what's going to expand to become the following body.
3:40
And when it's the description is what will show when it's when it's ready to be used. So if we go in here and we try again, we type in log.
3:49
You'll notice there's print to console. That's the description. I was talking about before I can click on it and it automatically created the console
3:58
log for me and that type of thing, right? Again, this is specific to other programming languages, the console log that we have here,
4:04
but we can change it to what we had here. Let's take an example of this copy it, paste it in here. You'll notice let's escape these characters.
4:23
And we'll set this to dollar one for placeholder. Get rid of that second one. And let's try it out now.
4:32
So now you can see it's log info, no episodes found for show ID and then it can type in Joe ID.
4:43
Variable like I was before and you can customize this further to your liking. Make things more dynamic if you'd like as well and have a lot of fun.
4:53
With it, you could see the benefit and value of code snippets in Visual Studio Code because it allows you to more quickly and efficiently generate and write your Python code.