Visual Studio 2019 Launch: Python development with Visual Studio

Apr 2, 2019 15:45 · 2349 words · 12 minute read

>> Welcome to Visual Studio 2019. My name is Tyreke White and I’m a Program Manager for Python Developer Tools in Visual Studio.

00:09 - The release of Visual Studio 2019 marks the introduction of some amazing new features to enhance the Python developer experience.

00:17 - These include the ability to bring your own pre-existing Python code through Open Folder support and without the need to create a Visual Studio project.

00:27 - We’ve introduced a Python toolbar which appears whenever you open a Python file, the toolbar allows you to create Virtual environments, as well as Conda environments, and manage packages within those environments.

00:40 - We now include Miniconda by default within the Visual Studio Installer.

00:45 - This decreases installation times enabling you to get started more quickly with your code and to create Conda environments without the need to install Anaconda.

00:56 - Finally, in this release of Visual Studio, you can now use live share to share your Python files with your peers and collaboratively work with them to debug your code.

01:06 - But showing is better than telling, so let’s dive in and explore these new features.

01:11 - So now let’s demo open folder support in the Python toolbar Visual Studio 2019.

01:16 - When you open Visual Studio 2019, you’re given the welcome homepage which allows you to get started with code by either cloning or checking out from a repo, opening a project or solution, opening a local folder or creating a new project.

01:32 - I’m going to open my Flask Web app that I’ve been working on which is here.

01:40 - So now that my Flask Web app is open, let’s explore it a bit more.

01:44 - As you can see, I’m given the solution explorer view here on the right-hand side and this shows me all of the folders and files that compose my project.

01:53 - An important thing to note about this project is the requirements. txt file.

01:58 - This includes all of the packages and dependencies that my project will need to function correctly.

02:04 - As well, these are the packages that IntelliSense will be using to provide autocompletion as well as syntax highlighting and all of that good stuff we know and love.

02:14 - So what we need to do now in order to be able to run this Flask app is to create a Virtual environment that has all the packages contained within this requirements. txt file.

02:24 - To do that we’re going to use the new Python toolbar, which is here.

02:28 - I’m going to click the drop-down arrow, I’m going to scroll down until I find add environment, click that and it brings up the add environment dialogue.

02:37 - From here, I can name my project, I can select the base interpreter for my project, let’s use Python 3. 7 and I can install packages from file.

02:47 - Notice that it’s finding that requirements. txt file I showed you a moment ago.

02:51 - But if I’m not satisfied with that file I can choose another by clicking here.

02:55 - By default, we select “Set as current environment”, because when I create this Virtual environment, I’m going to be loaded straight into it without the need to do anything additional.

03:05 - So let’s go ahead and create this flask_demo environment.

03:09 - Now that we’re loaded into our virtual environment, let’s explore it a little bit.

03:14 - A new feature that we’ve added is the manage package icon in this environment, which is located beside the Python toolbar here.

03:22 - Clicking this icon brings you straight into the Python environments tab.

03:26 - From here you can explore all of the different virtual environments you have created within your machine and you can see all the packages that are included within the current virtual environment.

03:37 - These should all look familiar because these are exactly the packages I installed from the requirements. txt file a moment ago.

03:44 - But if I had realized that a packages isn’t here that I want, we can use PyPI and simply type in the package name and install it.

03:54 - So what I’m going to do is pip install requests and voila we now have requests in our virtual environment and we can see it here.

04:07 - So now, let’s demo this Flask app. So what I’m going to do is, I’m going to add a date URL because I’m interested in going to a URL that tells me today’s date as well as the current time.

04:20 - To do that, I’m going to go to this routes. py file that I have here and I’m going to create a route for date.

04:26 - So let’s do that now and as you can see the IntelliSense starts kicking in.

04:39 - Let’s define date and let date be equal to get_date which is a function I pre-created at the top of this file.

04:51 - Finally, for this route, we need to return render_template.

04:55 - As you can see, the IntelliSense finds that and it tells me what I should pass in, so I need to pass in an HTML, which I’ve already created called date, as well as our date object we just created.

05:10 - I’m going to save this. Let’s start this application and I’m going to grab this URL here and I want to go to the date URL that we just created.

05:30 - Hello there, it’s Friday, March 29th, 2019 and it’s 10:54 AM.

05:36 - Now, let’s debug this date URL because I want to show everyone the cool features we have to offer for debugging.

05:46 - So to initiate a debugging session in Visual Studio, I can either press “F5” on my keyboard or I can press the green arrow here.

05:56 - So I’m going to press “F5” but you can press the green arrow.

05:59 - When I initiate that debugging session, it’s going to take me to the first breakpoint that I’ve set, which is my import statement at the top.

06:09 - With debugging in Visual Studio, you have all of the features we know and love, such as step into, step over, step out of.

06:17 - But, I have another breakpoint set inside of my date URL that I’m interested in debugging.

06:23 - So to continue to that next breakpoint again, I can press the green arrow or “F5” on my keyboard.

06:33 - Because I set the breakpoint inside of a URL, in order to actually hit that breakpoint with this Flask Web app, I’m going to have to go to the browser and type in that date URL once again, cool.

06:50 - Now that I’ve hit the breakpoint, you can notice that date equals get_date, where get_date is a function I’ve defined at the top of this file.

06:57 - So let’s actually step into this get date function to explore it a bit more, which I can do by going to my debugging toolbar and pressing “Step into” right here or “F11. ” So now that I’ve stepped into get_date, you can see that date is equal to datetime. now.

07:17 - What’s cool is that, now that I’m inside of this function, if I step over where I define date, notice that the locals window pane becomes populated with the date object as well as the value and the type which is a datetime object or I can also get this information by hovering over date and day 29th as well as the minute even down to the microsecond, pretty cool.

07:47 - If I want to step out of this function get_date, I have two options.

07:51 - I can either use the step out command, from the toolbar or I can scroll down to the line I’m interested in running to and notice that when I hover beside that line this green arrow appears and this is called the “Click-to-Run” button.

08:09 - I can press this and the app finishes to completion.

08:15 - Hello there, Visual Studio 2019 allows you to bring your own code no matter the scenario.

08:22 - So you also get all the same functionality even if the Python file is embedded within a larger project like a C++ project.

08:31 - What’s great is that their support for mixed-mode debugging and IntelliSense to make you even more productive in those scenarios.

08:39 - As I mentioned earlier, with Visual Studio 2019 you can bring any Python code no matter the scenario.

08:47 - So now what we’re going to do, is I’m going to show you guys a script called mandelbrot. py which creates and plots a Mandelbrot set.

08:58 - So as you can see I’m loaded to my familiar page, have the solution explorer and notice that mandelbrot. py has one package dependency and that’s NumPy.

09:09 - So again, I’m going to create a Conda environment for this package, but keep in mind that you can create a Conda environment for a different arrangement of packages.

09:21 - This one just only happens to use NumPy. So again, I’m going to go to the Python toolbar, go to Add Environment, but this time I’m going to select “Conda Environment” on the left-hand side.

09:33 - I’m going to name it, let’s call it test. I can add packages from a YML file, which is a popular Conda package file or what we’ve added is the ability to add one or more Anaconda package names by either typing them in the search bar here or using this icon.

09:51 - When you click this “Add Packages” icon, you’ll notice that we’ve bucketed the different popular Python workloads into things such as Data Science Core Libraries, which is where NumPy lives, Visualization, Machine Learning and a Web workload which has Django, Flasks, and Requests.

10:11 - So I’m going to select NumPy but also keep in mind that if this Conda environments or if this Mandelbrot script needed TensorFlow for example to run, I could type it in the box here and it would give me a preview of all the packages that will be installed within my Conda Environment and then I hit “Create. ” So now that I’ve created my Conda Environment called test, let’s do some more exploration.

10:34 - So if I go to the Python Environments tab, recall that I said that this Mandelbrot plot scripts is going to create and plot a Mandelbrot script.

10:43 - If I want to plot this in an IPython interactive experience, one thing I can do is find my Conda Environment test, go to this icon which opens the Interactive Window and drop down to this option which says use IPython interactive mode and hit the checkbox.

11:03 - So that way I can get the inline figures graphing that a lot of us no one love, nice.

11:11 - Now we’re ready to explore this script. In order to start running this script, there a couple of ways I can run it.

11:18 - I can select lines that I’m interested in from this code.

11:22 - So let’s select this “Import Statement”. I can right-click and find the option that reads, Send to Interactive.

11:31 - Notice that it sends the import statements to the Interactive Window and there are no errors which is great.

11:37 - Another way to run this is to select the lines that I’m interested in again but this time I can use control enter.

11:45 - As you can see within the Interactive Window, it sends the two code snippets to the window and it replies Hello World.

11:53 - Again no errors. To be even more productive in these interactive scenarios, another neat trick you can do is to decorate different sections of code with #%%, that way you can send these sections of code iteratively to the Interactive Window for further inspection.

12:13 - As you can see, I’ve decorated different sections with #%% and I’m going to send these chunks to the Interactive Window piece by piece.

12:22 - I can do this again by right-clicking over a section and selecting “Send to Interactive” or I can just use “Control Enter”.

12:30 - So I’m going to use “Control Enter”. We send that snippet as well as that snippet, this snippet, and that snippet.

12:39 - Notice the warnings that pop up but not a big deal at all.

12:42 - Now I’m ready to show you guys what this Mandelbrot plot actually looks like.

12:46 - So starting from the top I’m going to use “Control Enter” and everything to the Interactive Window.

13:00 - There it is, a Mandelbrot plot. What’s great about the inline figure graphing is that if I see something is off with my figure, I can compare my figure with my code and send it back to the Interactive Window.

13:16 - No need for additional pop-ups or dialogues for plots.

13:20 - Finally, there is now live share support for Python and Visual Studio 2019 to enhance your collaborative coding experience.

13:28 - In VS 2019 you can initiate a live share session by clicking the “Live Share” button on the upper right corner of Visual Studio to generate a link to share with your peers.

13:38 - Users who join your live share session will be able to see your Python files, see IntelliSense from your selected Python Environment and collaboratively debug through your Python code off from their own machine.

13:51 - There you have it, Visual Studio 2019 bring some powerful sleek tools that will enable you to bring your own Python code wherever you may be.

14:00 - Are you ready to get started? Check out the links here for more info. Happy coding.

14:05 - [MUSIC].