In this article, we will see how to set up Python in Visual Studio 2017 (I am using the free Community Edition) and later, we will create a sample web app with the help of Bottle framework (Bottle template is available in Visual Studio 2017).
Step 1 – Install Python in already installed Visual Studio 2017/New installation with Python
You can download the Visual Studio Community Edition online installer from this URL and run it. If you have already installed Visual Studio, it will check for the existing installation.
You can add more features to it if needed.
Please click the “Install” button and it will take some time to install all the new features.
Step 2: Details about the Python features available with Visual Studio 2017
There are two types of Python applications currently available in Visual Studio Machine Learning Category.
Step 3: Create a Bottle Framework Project
In this Polling Application, the users can give their vote to any topic they choose and later, they can view the total polls on that topic.
Choose the Python Bottle project template and give a name to that.
Our polls project needs some external packages so we must install these packages. As Microsoft recommends, we can install these packages in a virtual Python environment.
We must save our project now, before proceeding to the further steps.
We can give the environment a name. By default, the environment’s name is “env” but if needed, you can change it. I have given it as “python-env”.
Click the “Create” button now. It will take some time to create the environment and other dependencies.
Please note that there is currently one issue in Visual Studio Community edition – it will not add the new files to the solution the first time after project creation. It is listed as a missing file as shown below.
If you click any file in the Solution Explorer, it shows the below error message.
For the time being as a workaround, we can close the solution and reopen it.
Bottle framework is an MVC kind framework that contains Models, Views, and Controllers. In our project, we can see various Python files under the Models folder and Views folder. We have some controller files also. Routes.py is the main controller file and it will control the routing part of our application. If you look at the requirements.txt file, you can see there are three references it contains.
Step 4: Installing the required libraries from requirment.txt
Step 5 – Running the Python Bottle Application
Please run the application. Visual Studio will automatically create a web server (like IIS local server) and it listens to every action from the browser and the log.
Please edit the settings.py file with the following changes. You must change repository name from “memory” to “mongodb” and then change MONGODB_HOST to “localhost:27017” (We are using Local MongoDB database so the host is localhost and default port is 27017)
If you have MongoDB Compass Community or any other MongoDB data explorer tools available, you can see a new “polls” database and “polls” collection created now.