The latest release of Visual Studio marks some fantastic new features to enhance the Python developer experience. It includes the ability to bring your existing Python code form Open Folder support without the need to create a Visual Studio project.
It also introduced a Python toolbar which appears when you open a Python file in the toolbar that allows you to create virtual environments as well as Conda environments and manage packages within those environments.
Now Miniconda is included by default in the Visual Studio Installer. By this, our installation time decreased, which enables us to get started more quickly with your code and to create Conda environments without the need to install Anaconda.
In this release of Visual Studio, we can use live share for sharing our Python files with your peers for working with them to debug your code.
Let’s dive and explore new features.
Open folder support in the Python toolbar of Visual Studio.
When you open Visual Studio, you have a welcome homepage that allows you to get started with code by cloning or checking out from a reopening a project. Open a local folder to create a new plan.
Now we will be going to open the Web app that we’ve been working on, which is here.
They have given the solution explorer view here on the right-hand side, and this shows us the folders and files to create our new project.
One important thing to note about this project is the requirements.txt file, which includes all of the packages and dependencies that a project will need to function.
Well, these packages IntelliSense will be used to provide auto-completion and syntax highlighting and all of that good stuff we know and love.
So what we need to do to be able to run this app. We have to create a virtual environment that has all the packages contained within these requirements.txt files, and for this, we’re going to use the new Python toolbar.
Click the drop-down arrow, scroll down until you find an environment.
Just Click, and it brings up the add environment dialogue.
From here, you can name your project, and you can select the base interpreter for your project.
You can install packages from the file. Can you notice that it’s finding that requirements.txt file we showed you a moment ago?
But if you are not satisfied with that file, you can choose another by clicking here. By default, we can select “Set as current environment.” When you create this Virtual environment, you are going to be loaded straight into it without the need to do anything additional.
Let’s go ahead and create a demo environment and get loaded into our virtual environment.
A new feature that is added to manage package icons in this environment is located beside the Python toolbar.
Click the icon; it will bring you straight into the Python environments tab.
From here, you can explore different virtual environments that you have created within your machine. You can see all the packages that include the current virtual environment.
It should all look familiar because these are precisely the packages we had installed from the requirements.txt file a moment ago.
But if you had realized that a package isn’t here that you want, you can use PIP. Simply type the package name and install it.
What we are going to do in PIP install request and voila you can now request in your virtual environment, and see it here.
What we are going to do is, we will add a date URL because our interested URL is going to tell us today’s date as well as the current time.
Go to route the PIP file that we have, and we are going to create a route for the date.
Let’s do that now
As you can see, IntelliSense starts kicking.
Let’s define the date and let the date be equal to get_date, which is a pre-created function at the top of the file.
Finally, this route needs to return render_template. As you can see, IntelliSense finds it, and it tells us what we should pass in, so we need to give in an HTML which we’ve already created called date as well as our data object we just created.
Just grab the URL and go to the date URL we just created.
It’s Friday, Feb 29th, 2020, and it’s 10:54 AM.
To show you the cool features for debugging.
To initiate debugging session in Visual Studio, press “F5” on the keyboard, or push the green arrow.
When you initiate that debugging session, it’s going to take us to the first breakpoint set, which is an import statement on the top.
With debugging in Visual Studio, you will experience features like
- Step into
- Step over
- Step out of
But, another breakpoint set inside of the date URL is the interest in debugging.
To continue to that next breakpoint again, press the green arrow or “F5” on the keyboard.
Set a breakpoint inside URL and to hit breakpoint with Web app, go to the browser, and type date URL again.
Hit the breakpoint, and you will notice that date equals get_date where get_date is a function defined at the top of this file.
Let’s step into this get date function.
To explore it a bit more, go to the debugging toolbar and press “Step, into” or “F11”.
Now that you’ve stepped into get_date, you can see that date is equal to date-time.
Now we are in this function, and if we step over, date notices that locals windowpane becomes populated with the date object or the value and type of date-time object, or it will get this information by hovering over date and day. The minute even down to the microsecond.
If we want to step out of this function get_date, we have two options.
We can either use the step out the command from the toolbar or,
We can scroll down to the line interested in running and notice that hover beside that line, this green arrow appears.
It is called the “Click-to-Run” button. Click!
It’s Friday, Feb 29th, 2020, and it’s 10:54 AM.
Visual Studio allows you to bring your code. No matter what the scenario is, you will get all the functionality even if the Python files are embedded within a more substantial project like C++.
What’s great is that the support for mixed-mode debugging and IntelliSense to make you even more productive in those scenarios.
As mentioned earlier with Visual Studio, you can bring any Python code no matter the scenario.
What we’re going to do now is to show you a script called mandelbrot.py, which creates and plots a Mandelbrot set.
As you can see, the loaded familiar page has the solution explorer, and if you notice mandelbrot.py has only one package dependency, and that’s NumPy.
Let’s go and create a Conda environment for the package; just keep in mind that you are creating a Conda environment for a different arrangement of boxes.
This one just only happens with NumPy. Again, to go to the Python toolbar, just go to the added environment, but this time select, “Conda Environment” from the left-hand side.
Let’s call it a test.
Add packages from the YML file, which is accessible. Conda package file or have added the ability to add one or more Anaconda package names by typing them in the search bar and using the icon.
Click the “Add Packages” icon.
Note: The different popular Python workloads are bucketed in one place. Like Data Science Core Libraries, where NumPy lives, Visualization, Machine Learning, and a Web workload, which has Django, Flasks, Etc.
Select NumPy, but this Conda environment or if this Mandelbrot script needed TensorFlow, for example, to run, type it in the box. It would give us a preview of all the packages that will be installed within the Conda Environment.
Now the Conda Environment is created, let’s name it a test.
The Python Environments tab recalls this Mandelbrot plot script going to create and plot a Mandelbrot script.
If you want to plot this in an IPython for an interactive experience, you can do one thing, find the Conda Environment test.
Go to the icon which opens in Interactive Window.
Drop down to option, which says use IPython interactive mode.
Hit the checkbox.
In this way, you can get the inline figures graphing.
Now get ready to explore the script.
Start running the script, and you have a couple of ways to run it.
You can select lines that are interested in this code.
Let’s select “Import Statement.”
Right-click and find the option that reads Interactive.
Note: It sends the import statements to the Interactive Window, and there are no excellent errors.
Another way to run this is to select the lines that are interested in again, but this time you can use control + enter.
As you can see within the Interactive Window, it sends the two code snippets to the window, and it replies:
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.
As you can see, decorated different sections with #%% and to send these chunks to the Interactive Window piece by piece.
You can do this again by right-clicking over a section and selecting “Send to Interactive,” or you can just use “Control Enter.”
Note the warnings that pop up. Ignore!
It is so starting from the top use, “Control Enter,” and everything to the Interactive Window. There it is, a Mandelbrot plot.
What’s great about the inline figure graphing is that you can see something is off with figure. You can compare your figure with your code and send it back to Interactive Window.
You don’t need any additional pop-ups or dialogues for plots.
Finally, there is now live share support for Python for gis and Visual Studio to enhance your collaborative coding experience.
In Visual Studio, you can initiate a live session for sharing or by clicking the “Live Share” button. See on the upper right corner of Visual Studio to generate a link to share with your peers.
There you have Visual Studio to bring some powerful, sleek tools that will enable you to bring your Python code wherever you want.