I have a script, where you insert list of devices, config file, username, password and then goes through all devices and apply configuration via SSH/CLI with netmiko module.
There are more nuances as creation of log files, but this is basic idea.
My problem is, that when first device in the list is reached, script will connect to the device and then script immediately rerun. After rerun, it again goes to first device in list and this time also configure it and continue to other devices in list with no problem and no additional reruns.
It seems, that this line cause rerun of the script:
How can I avoid to script rerun when Netmiko ConnectHandler is used? What exactly cause this rerun and why only when first device is reached?
Another interesting thing is, that script is run via button and after rerun this button stays as “True”, and it works fine.
Welcome to the Streamlit community and thanks for your detailed question! It seems likely that your script reruns when you call ConnectHandler from Netmiko because Streamlit reruns the entire script from top to bottom on any widget interaction or when certain exceptions or state changes occur. This can be especially tricky when using buttons and external libraries that may block or alter the script’s flow.
The behavior where the button stays “True” after rerun is expected: st.button returns True only on the rerun triggered by its click, and then resets to False on the next rerun. If your connection or SSH call causes a rerun (possibly due to an exception, blocking call, or thread/context issue), the script restarts, and the button logic may not behave as you expect. To avoid this, use st.session_state to track the button state and progress, and ensure your SSH logic is robust to reruns. For more on button and rerun behavior, see Button behavior and examples and Execution flow.
Would you like a step-by-step example of how to structure your code to avoid this rerun issue and manage state with st.session_state?