VSCode's Python Interactive mode is AMAZING!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone so i've been gone a long time now and i decided uh if i'm gonna make a comeback video if you will i want it to be a simple one so today i'm gonna talk about one of my favorite features in visual studio code probably the only thing that keeps bringing me back to this editor even though my default editor is emacs and that is the python interactive mode in visual studio code now before we talk about the python interactive mode itself i want to take a moment to talk about the fact that visual studio code will actually open ipython notebooks just natively this is a relatively recent feature i think one year or something like that and to do that we can just do like open a file and then i have this running code example notebook that i got from the internet if i just click on that it's going to do a few things first you're going to see the json and then it's going to run the notebook and what it's doing actually is in the background it is running a jupyter server and then it is using that to serve this notebook effectively the same way that a browser might and then if you wanted your work to be inside a particular python virtual environment you actually get the ability to select the virtual environments similar to how you might be doing it in a normal python workflow in vs code note that to get to this point all i had to do was install the official python extension vs code handles basically everything else now you can see vs code shows me all the available global python environments and then i also made a virtual environment called test and that option is there as well this notebook functions almost exactly like a normal notebook so if i just hit shift enter on a cell it'll run that cell or render markdown i can run this cell and then the value of a will be set to 10 printouts show up as you expect and yeah it works just like a normal notebook now i love notebooks for prototyping and i use them or used to use them all the time but this feature that i'm going to talk about the python interactive mode has completely removed notebooks from my workflow so if i go ahead and open a new file and just save that as a python file this is a standard python file right now there's not going to be anything special about it until i make this special comment and all of a sudden vs code gives me these options i can run this cell i can run this cell and run the cells below or i can debug the cell this looks an awful lot like a notebook so if i go ahead and write let's say import os i did a control enter there which is standard notebook shortcut for running the cell but not advancing and what vs code does now is that it throws up this interactive window on the right and then in that window it actually ran the command that was in that cell i can go back to the cell and let's say do shift enter and it'll run that command again it'll show me what that command is and if if it has any output we will see the output as well and then there's also this icon that you can click to go to the code the the particular cell that generated that output so you can sort of keep these two pieces the code on the left and the output on the right in sync to some degree now if i were to write a command that produces an output for example os.list tires that output gets printed on the right as well this thing on the right it's like a normal ipython shell on the bottom i actually get a prompt and it says you know you can type code here and press shift enter to run very similar to writing something in a cell so great we have a very basic text file just a normal.pi file but we can interact with it we can separate it out in the same way as a notebook this is amazing to me and i hope you're excited as well now i'm pretty bad when it comes to abusing ipython magics and one of the cool things that this mode does is that you can actually keep those magic so the one unholy line that no one should ever ever run but we all run anyway pylab inline you can just run that oh and i get an error when i try to do that because i was not in the right virtual environment you can switch the virtual environment by clicking on this button on the lower left if i click on that it gives me the same options that i was showing before i want the test virtual environment because i have matplotlib installed in that so i click that and actually before we can run the cell again we have to restart the ipython kernel because we changed our virtual environment so we do that it says all variables will be lost i usually set that to yes and don't ask again and then because the output on the right has kind of started getting kind of clutter we can use this button this x mark to remove all the cell output and then we can run the cell again and now the magic works it populates the interactive namespace we get everything that we want and then we can do stuff that we would normally be doing in a notebook so make a plot and the plot shows up on the right it is in line and one of the nice benefits of using this is unlike in ipython notebook this plot is actually not being rasterized into something like a jpeg or a png instead instead it stays as an svg so we can actually zoom in and look at sort of the details so the basic main stays off cell-based execution of code and plotting and stuff like that works also there's special support for pandas when your output contains a pandas dataframe it gets formatted nicely and then one of the really cool things that you get here is also you can list all the variables that have been defined in this notebook right now we don't actually have any variables defined but if i set this data frame equal to something then i see what the variable is what the type is some information about it what its value is etc and i can double click on it and for data frames we also get this nice little data viewer where you can sort of sort things you know you kind of get an excel ish output almost for free which is kind of nice and then finally one of the really cool things that you can do here is because this is vs code we can go ahead and set breakpoints visually and then click debug cell and that launches this whole really nice visual debugger where you can view sort of all the variables that currently exist you know the locals and the globals for the python interpreter you can view the call stack kind of jump through it etc we can put watches on variables and then step through the code so yeah that's pretty much it you get in my opinion pretty much all the benefits of having a notebook but you can keep a normal text file that can be very quickly turned into a module and packaged and deployed and that's really really useful to me i hope this has also been useful to you thank you very much for watching this video i hope you have been staying safe if you're new to the channel please consider subscribing and liking and until next time bye
Info
Channel: Jack of Some
Views: 346,460
Rating: undefined out of 5
Keywords: python, vscode, vscode python, python interactive mode, vscode jupyter, vscode notebook
Id: lwN4-W1WR84
Channel Id: undefined
Length: 6min 58sec (418 seconds)
Published: Fri Aug 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.