Search code examples
pythonuser-interfaceexceptiontracebacktaipy

How to retrieve the full traceback in Taipy when an exception is raised?


I'm encountering an issue while working with Taipy, where exceptions raised by the framework don't provide the complete traceback information like Python does by default. This lack of detailed traceback makes it difficult for me to debug my code effectively. Is there a way to obtain the full traceback when an exception is raised in Taipy?

Here's an example scenario to illustrate the problem:

from taipy.gui import Gui, notify

def raise_error(state):
    temp = 10/0

md = """
<|Raise error|button|on_action=raise_error|>
"""


Gui(md).run()
TaipyGuiWarning: on_action(): Exception raised in 'raise_error()':
division by zero
TaipyGuiWarning: on_action(): 'raise_error' is not a valid function.

In the code above, Taipy gives me basic information on the Exception but I would want to have all the details displayed in the console. Is it possible?


Solution

  • You can catch an exception by creating an on_exception function. This function is called whenever an exception occurs in Taipy.

    Then, you can display the stacktrace (and even notify the user of the exception if needed):

    from taipy.gui import Gui, notify
    import traceback
    
    def raise_error(state):
        temp = 10/0
    
    md = """
    <|Raise error|button|on_action=raise_error|>
    """
    
    def on_exception(state, fct_name, e):
        notify(state, "error", f"Error in function {fct_name}: {e}")
        print(''.join(traceback.format_exc()))
    
    Gui(md).run()
    

    You could use logging for a better management of logs.