Jupyter Notebooks in VS Code Walkthrough

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
are you interested in working with jupiter notebooks but you just don't even understand all that they can do then you've come to the right place because today i'm going to walk you through using jupiter notebooks in vs code i'm going to be using a public github repo so you can follow along right with me this is the repo that we're going to be working with it was created by claudia apm at microsoft and to follow along you can go ahead and find it at this link if you scroll on down to the readme this is going to detail some of the setup steps that you're going to need to have this smoothly run within vs code specifically you're going to need to install anaconda and run a couple of commands and then connect to that con environment from within vs code now i won't be detailing that setup in this video but you are in luck because we do have another video that walks you through setting up anaconda with your jupiter notebooks and just a quick note you don't have to use anaconda to run jupiter notebooks in vs code but it does help out a lot in instances where your notebook might require a lot of dependencies or common python packages so in this instance it's really helpful all right so now let's head on over to vs code and take a look at this notebook so for this to run in my vs code setup i need to have the python extension installed and in turn that's going to also install the jupiter notebook extension and then i'm going to navigate over to the titanic notebook file and i also am already connected to the conda environment that i created with the initial setup and that's going to be called the gold and scenario environment before we start diving into running this notebook i want to do a really quick overview of the notebook experience in bs code and what the specific notebook does so not the most upbeat of topics but this notebook is going to be using machine learning algorithms to try to predict whether someone would survive the sinking of the titanic based on attributes in a data set if we take a look at our files this titanic data.csv file is going to be the data set that we are reading from and yes this is real world data now looking at the notebook there are several sections says notebook which you can see in this outline view in the bottom left of the screen and if we take a quick scroll through these we can see that this outline is generated from the markdown headers within this notebook so if i click on one of these headers we can see that it is just a markdown cell and to show that rendered markdown you can just hit this check mark here now when you're working at jupiter notebooks in vs code a lot of your time is going to be spent with this top toolbar up here you can add a new code cell and when i do that it automatically detects that i am using python as my language you can add a new markdown cell which again is going to show that markdown language in the bottom right you can run all cells which we are going to see in just a little bit clear the outputs of all your cells you can restart or interrupt your kernel you can open your variable explorer and your outline view and in the more actions there's a couple of customizable options there and you can also export files finally in the top right you will see the kernel that you are currently connected to and if you were to click that you could change that to the kernels that you have available i'm going to go ahead and kick off running this notebook by clicking the run all button on this toolbar so there are a lot of different cells in this notebook and i don't really want to have to scroll through to figure out what cell is running so you can see that in this toolbar as soon as i started running cells this nice go to running cell button popped up so i just click that and it's going to take me right to where the currently running cell is so a very quick way to navigate while you are running a notebook with multiple cells now that this has finished running one thing that we can do is open the variable explorer and this is going to do is pull up an entire list of all the variables that were defined during our notebook run so you can sort them by name by type however makes it easiest for you to visualize all of your variables and the other cool thing is that we have something called the data viewer so if we take a look at any of these variables that have this little pop-out icon next to it we can actually drill even deeper into that variable to see what exists in that variable so for example i'm going to take a closer look at this titanic data frame click this pop out here and the data viewer will open in a new tab and you can see i can now visualize this entire variable everything that is inside of it i can sort on various columns in the data frame i can filter on any of the different columns so that way it only gives us the data we're looking for and really this is just a great way to drill even deeper into the variables that you have access to from your notebook vs code provides great data visualization functionality for things like charts graphs and trees in jupiter notebooks i've skipped ahead to the decision tree section of this notebook and if we scroll down just a little you'll see we have this visualize tree method which prints out a nice image of our decision tree if you hover over then in the top right you'll see the copy to clipboard option the expand image or you can save this image to your file system i'm going to expand this image and we can see that a new tab pops up with that image in it and when that pops up you can interact with that image you can zoom out zoom in and drag around so that you can further inspect everything that is in this decision tree alright so let's head on back to the notebook so when i ran this notebook i did it using the run all functionality which ran all the cells in the notebook but you probably don't always want to run every single cell in your notebook i'm going to show you a couple of other ways that you can run your cell the first being that to the left you have this nice little play icon which lets you just execute that cell and then over to the right here we have a run by line option which when i click that icon we can see that it highlights the current line that i'm on and then i can either hit f10 or just continue hitting that run next line icon to then step through that entire cell so another really easy way to skip through there and then if you just want to you know stop running line by line you can click the continue execution to just go through the rest of the cell other options you have here for running is this execute above cell so that's going to execute all the cells above your currently highlighted cell and then you have the option to execute the current cell and all the cells below you can also do things like add break points and then debug your cell so that way you can run through and it will stop on any break points that you have specified now if you ever want to move any cells around it's very easy all you would do is just grab and hold this blue bar next to the cell and then drag it to wherever you need to go so one more time i'm clicking this blue bar i'm going to drag this up and stick it back up there and then if you want to collapse the cell all you have to do is then just single click that blue bar and that works for both the cell and that cell's outputs so i've been using the outline view in order to easily navigate among this notebook but that's not the only navigation technique that we have available in your notebook you can also use search functionality so i'm on a windows machine so i'm going to hit control f but you can also hit command f if you're on a mac and that's going to pop up this find navigation item in the top right and then i can just type in whatever search term i have and that's going to give me various results and you can also filter this based off of what exactly you want to be searching for so right now it's just going to be looking for that cell source wherever that decision happens if we add the cell output into that filter we can see we now have 10 results instead of 7 results and that's not gonna also show anything in the output and not just the source and then we can also add rendered markdown to that and we can see again that changes the number of search results available to us because we are changing that filter now one of the coolest things that we have available with vs code and jupyter notebooks is intellisense so i'm going to create a new code cell here and just show you what that intellisense looks like so i can start typing titanic data frame you can see i'm already getting intellisense popped up there i can just do dot and then let's say i want to do group by if i begin typing that it says oh hey do you mean group by yep i sure do i can sort and that will pop up any sort methods i have available to me on this variable and i can do things like getting different values so it just makes it really easy for you to have that native intellisense that's available to you in normal python programming but it's available to you now within these jupiter notebooks so it makes it a lot easier to track any of your errors and just really quickly code up a new cell all right so i've made these changes to this notebook file and i want to take a look at what that looks like in the source control explorer so vs code does a great job of tracking changes made to notebook files within source control if we scroll down the first change that was tracked is this output which just resulted from some warnings i got when i ran through the import statements and this differs from the file checked in so it's being tracked as a change and it's really easy to either revert this change with just a click or i can simply just collapse that output and continue through viewing the differences we scroll down a little further you can see this new cell that i added is very clearly marked you can see that there's no cell before it and now we have the green to indicate that this was a cell that was added so finally after you've created this data analysis everything looks good you might want to be able to export it as a different file so that you can easily share it with others so i can easily do that in vs code with these three dots that go to the more actions in the toolbar and clicking the export option and you can see i have been prompted to export it as a python script html or pdf file so this is a very quick and easy way for you to save your notebook in a different format now that's just the start of everything that you can do with vs code and jupyter notebooks from data visualization to easy navigation to intellisense if you're working with jupyter notebooks vs code is the place to be and we have plenty of other jupiter content on our channel like five things you didn't know that you can do with jupiter notebooks so make sure to like this video and subscribe to our channel so that you can stay in the loop of all things vs code happy coding
Info
Channel: Visual Studio Code
Views: 79,224
Rating: undefined out of 5
Keywords: vscode, visual studio code, vs code
Id: DA6ZAHBPF1U
Channel Id: undefined
Length: 10min 40sec (640 seconds)
Published: Fri Sep 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.