icon: LiWrench
Title: Debugging Streamlit Apps in VS Code
As we build Large Language Model (LLM) applications using Streamlit, it's important to be aware and have the right tools and techniques to streamline our development process. One such tool is the Python Debugger in Visual Studio Code (VS Code).
In this note, we'll explore the importance of being able to debug the Streamlit code, how it enhances our development workflow, and provide a step-by-step tutorial on setting it up in VS Code.
✦ A common misunderstanding is that debugging only applicable to lousy programmers or only needed when we have bugs.
✦ Debugging is a fundamental aspect of software development. It allows us to:
Let's dive into the practical steps of setting up and using the Python Debugger in VS Code for our Streamlit app.
Here, we assume we all have installed and set up our Visual Studio code correctly for Streamlit app development. For details on setting up, see 3. Setting Up Streamlit Project with pip and venv from Topic 7.
Install the Python Debugger
extension developed by Microsoft.
To configure the debugger, we need to create a launch.json
file in our project. This file contains the configuration settings for debugging.
Ctrl+Shift+D
.launch.json
file will be created with a default configuration. Modify it to match the following configuration:{
"configurations": [
{
"name": "Streamlit Debug",
"type": "python",
"request": "launch",
"module": "streamlit",
"args": [
"run",
"${file}"
]
}
]
}
Breakpoints allow us to pause the execution of our code at specific lines, so we can inspect variables and the program state.
Now that we have configured the debugger and set breakpoints, we can start debugging.
F5
to start debugging.VS Code will launch the Streamlit app and pause execution at the breakpoints we set. We can now inspect variables, step through the code, and evaluate expressions in the Debug Console.
While debugging, we can use the following controls:
F5
): Resume execution until the next breakpoint.F10
): Execute the next line of code, but don't step into functions.F11
): Step into functions to see their execution.Shift+F11
): Step out of the current function.Ctrl+Shift+F5
): Restart the debugging session.Shift+F5
): Stop the debugging session.We can also hover over variables to see their current values or use the Variables pane to inspect them.
00:00 - Intro and Setup "Python Debugger" extension
01:45 - Configure the Debugging Session (launch.json)
03:15 - Set Breakpoints in the code
06:46 - Starting the Debugging Session
10:08 - Examine the variables and their values
14:41 - Jupyter Notebook-like Interactive Python session (Debug Console)
16:32 - Navigating the code in Debugging Session (Continue, Step Over, Step into, and etc)