-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlit_debug.py
72 lines (68 loc) · 2.56 KB
/
streamlit_debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# How to use:
#
# import streamlit_debug
# streamlit_debug.set(flag=False, wait_for_client=True, host='localhost', port=6789)
#
# Requires corresponding entries in launch.json:
#
# {
# // Use IntelliSense to learn about possible attributes.
# // Hover to view descriptions of existing attributes.
# // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
# "version": "0.2.0",
# "configurations": [
# {
# "name": "Python: Current File",
# "type": "python",
# "request": "launch",
# "program": "${file}",
# "console": "integratedTerminal",
# "env": {"DEBUG": "true"}
# },
# {
# "name": "Python: debugpy Remote Attach",
# "type": "python",
# "request": "attach",
# "connect": {
# "port": 6789,
# "host": "127.0.0.1",
# },
# "justMyCode": false,
# "redirectOutput": true,
# "logToFile": true,
# // "debugAdapterPath": "${workspaceFolder}/src/debugpy/adapter",
# },
# ]
# }
#
# When the streamlit_debug flag=True and wait_for_client=True, you'll need to activate "Python: debugpy Remote Attach" debug session.
#
import streamlit as st
import logging
_DEBUG = False
def set(flag: bool=False, wait_for_client=False, host='localhost', port=8765):
global _DEBUG
_DEBUG = flag
try:
# To prevent debugpy loading again and again because of
# Streamlit's execution model, we need to track debugging state
if 'debugging' not in st.session_state:
st.session_state.debugging = False
if _DEBUG and not st.session_state.debugging:
# https://code.visualstudio.com/docs/python/debugging
import debugpy
if not debugpy.is_client_connected():
debugpy.listen((host, port))
if wait_for_client:
logging.info(f'>>> Waiting for debug client attach... <<<')
debugpy.wait_for_client() # Only include this line if you always want to manually attach the debugger
logging.info(f'>>> ...attached! <<<')
# debugpy.breakpoint()
st.session_state.debugging = True
logging.info(f'>>> Remote debugging activated (host={host}, port={port}) <<<')
if not _DEBUG:
# logging.info(f'>>> Remote debugging in NOT active <<<')
st.session_state.debugging = False
except:
# Ignore... e.g. for cloud deployments
pass