Analyze Python Code in Jupyter Notebooks

We present a new tool that integrates modern code analysis techniques with Jupyter notebooks and helps developers find bugs as they write code.



By Julien Delange, CEO of Codiga

Image

 

Summary

 
 
Jupyter notebooks do not support traditional code analysis tools, which makes it harder to find bugs. We present a new tool that integrates modern code analysis techniques with Jupyter notebooks and helps developers find bugs as they write code.

 

Why verifying Python code?

 
 
Python became the most popular programming language (it’s ranked 1st in the TIOBE index). Even such a popular language can be error-prone, especially because of its nature (an interpreted language with dynamic typing ) since its control flow is defined by the indentation. In a 2018 study, Python was found to be one of the most error-prone languages (with C++, C, and Objective-C). As with other programming languages, Python has its own pitfalls that developers should avoid (and have been fixed over the years).

For these reasons, it’s always good practice to use code analyzers that check your code and detect potential errors. The primary objective is to help you find any potential errors before shipping your code to production. But it also helps you to follow good coding practices.

 

Existing ecosystem

 
 
Most IDE comes with some code analysis capabilities that validates the syntax of your code. For example, PyCharm comes with analysis capabilities that check for syntax and semantic issues. There are also multiple open source tools that check Python code either from a semantic (e.g. pylint), security (e.g. bandit), or style (e.g. black) perspective. These tools are often integrated into IDE using custom plugins and extensions.

However, static analysis tools are not integrated with Jupyter notebooks and developers cannot benefit from the analysis of such tools. 

 

Checking code in Jupyter notebooks

 
 
As Python developers that regularly use Jupyter for data analysis, we wanted to bring these tools into the Jupyter ecosystem and help developers catch bugs quickly. We implemented a Chrome plugin that analyzes Python code in Jupyter notebooks and reports all issues while developers are writing code.

The tool executes static analyzers such as Pylint and Bandit to detect syntax, semantic and security errors in Python code and reports the errors directly into the Jupyter notebook. The plugin is currently compatible with Jupyter notebooks and will soon support other platforms such as Google Colab or AWS Sagemaker.

Image

 

Conclusion

 
 
Python is the most popular programming language today but writing flawless Python code is hard. Thankfully, multiple tools exist to help developers detect sub-optimal code. By interfacing these tools with Jupyter notebooks, our Chrome extension helps developers detect issues as they write code and fix them quickly, before shipping code into production.

 

Resources

 

 
Bio: Julien Delange is the CEO of Codiga (formerly Code Inspector), a company that helps developers write better code faster. Julien is an experienced software developer and has worked at Twitter, Amazon Web Services, and is the author of the book Technical Debt in Practice published by MIT Press.

Related: