Data Science with Python in Visual Studio Code

Rong Lu

Rong

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

Defining cells in code using #%% allows you to run cells in the Python Interactive window. Markdown and graphical plots are displayed.

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:

  1. 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]”.
  2. “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.
  3. 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.
  4. 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.

Jupyter notebooks are converted to Python code, with cells defined using #%%

Try it out today

We’re rolling out these features as experimental in the latest Python extension for Visual Studio Code that shipped today, be sure to check out our Jupyter code cells docs for instructions on getting started. 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!  😊

Rong Lu
Rong Lu

Principal Program Manager

Follow Rong   

6 comments

  • Avatar
    Gary Mialaret

    Great article!
     
    By the way, are there plans to include a variable explorer to go with the scientific environment in the future? It is one of my favorite features of Spyder and PyCharm!

  • Avatar
    Sudeep Mandal

    I love VSCode, and while this is a nice step, it is still sorely missing a first-class REPL experience when it comes to Python. It would be great if it could have a mix of features from Spyder (All code executed in a live iPython console that is persistent and can be interacted with at any point, ability to use cell magics to eval blocks of code in the console, a good variable viewer for typical datatypes such as ndarrays and DataFrames) and Pycharm (Amazing debugging experience due to the IPython-like console that is fully populated with the stack at debug time).
    The current debugger is nice, but limited, especially in only being able to run single lines of code interactively which is a severe limitation.
    Also, I should note that supporting a Jupyter Notebook like experience is not the same as implementing a good REPL experience. The 2 serve drastically different purposes, with the latter being far more critical for rapid prototyping, especially when doing data-heavy coding.
    Thanks to all the VSCode team for all the hard work on this product. It’s outstanding and every update feels like Christmas all over again! I hope you guys are able to address these pain points in future releases. Nothing would make me happier than to not have to fire up Spyder/Pycharm every time I need to work in Python land.

    • Avatar
      Sean Violante

      You should update the extension.most of the features you mentioned are currently available in the interactive terminal (as opposed to regular terminal). What’s missing is variables explorer and debugger

Leave a comment