This post was written by Rong Lu, a Principal Program Manager working on Data Science tools for Visual Studio Code
Today we’re very excited to announce the availability of Data Science features in the Python extension for Visual Studio Code! With the addition of these features, you can now work with data interactively in Visual Studio Code, whether it is for exploring data or for incorporating machine learning models into applications, making Visual Studio Code an exciting new option for those who prefer an editor for data science tasks.
These features as currently shipping as experimental. We’re starting our Visual Studio Code investments in the data science space with two main use cases in mind:
- Exploring data and experimenting with ideas in Visual Studio Code. Just like how you would use Jupyter Notebooks to explore data, with Visual Studio Code you can accomplish the same but using a familiar editor with your favorite settings. You can define and run individual cells using the IPython kernel, visualize data frames, interact with plots, restart kernels, and export to Jupyter Notebooks.
- Import Jupyter Notebooks into Python code. When it comes time to turn experimentation into reproducible, production-ready Python code, Visual Studio Code can make that transition very easy. Run the “Import Jupyter Notebook” command in the editor and code will be extracted into a Python file, then all the rich features that make you productive are at your fingertips – including AI-powered IntelliSense (IntelliCode), integrated debugger, Visual Studio Live Share, refactoring, multi-file management, and Git source control.
Now, let’s take a closer look at how Visual Studio Code works in these two scenarios.
Exploring data and experimenting with ideas in Visual Studio Code
Above is an example of a Python file that simply loads data from a csv file and generates a plot that outlines the correlation between data columns. With the new Data Science features, now you can visually inspect code results, including data frames and interactive plots.
A few things to note:
- Just like how you organize Jupyter Notebooks using cells, you can define code cells in your Python code by using “#%%” and Markdown cells by using “#%% [markdown]”.
- “Run cell” links start to light up once “#%%” are detected. The first time the “Run cell” link is clicked (or Shift-enter are pressed), the “Python Interactive” window on the right will show up and a Jupyter server will start in the background. Code in the cells will then be sent to the Jupyter server to execute and results will be rendered in the window.
- By using the top toolbar in the Python Interactive window, you can clear results, restart the iPython kernel, and export results to a Jupyter Notebook.
- You can navigate back to the source code by clicking on the “Go To Code” button in each cell.
Import Jupyter Notebooks into Python code
If you have existing Jupyter Notebooks that are ready to be turned into production-ready Python modules, simply bring them into Visual Studio Code by running the command “Python: Import Jupyter Notebook”: this will extract Python code as well as Markdown blocks from the notebook, and put everything into a Python file.
Here is an example of a Jupyter Notebook and the generated Python file. Each code cell becomes a code section with annotation “#%%”, and each Markdown cell turns into a comment section with “#%% [markdown]” annotation. Both cell types are runnable in Visual Studio Code, which means you can reproduce the exact same results that you would see in a Jupyter Notebook.
Try it out today
We’re rolling out these features as experimental in the latest Python extension for Visual Studio Code that shipped today. Please give it a try and let us know what you think by taking a 2-minute survey to help shape the features for your needs.
A quick side note, this is an evolvement from the Visual Studio Code Neuron extension that we worked along with students from Imperial College London this summer. The Neuron extension received a lot of positive feedback just recently, and now we’re taking their awesome work further by building more of such capabilities into the Python extension.
Have fun playing with data in Visual Studio Code!