Function executing twice

I have a function that takes an array as a parameter and displays just the first value of the array. But in my console, it displays twice. Here is the function

    def add_annotation(self, data_array, fig):
        
        if fig is not None:
            x_pos = data_array[0]['min'][0]
            y_pos = data_array[0]['min'][2]
            ps = float(data_array[0]['ps'])
            mds = float(data_array[0]['mds'])
            hill = data_array[0]['id']
            annotation_text = f"{hill} <br> PS: {ps:.2f} <br> MDS: {mds:.2f}"
            
            print(x_pos, y_pos)
            fig.add_annotation(
                x=x_pos,
                y=y_pos,
                xref="x",
                yref="y",
                text=annotation_text,
                showarrow=True,
                font=dict(family="Courier New, monospace", size=16, color="#FFFFFF"),
                align="center",
                arrowhead=2,
                arrowsize=1,
                arrowwidth=2,
                arrowcolor="red",
                ax=50,
                ay=-90,
                bgcolor="black",
            )

And here is my output
image
Please any idea of what is happening?

You have not provided any of the code that calls that function which is where the problem is I assume.

However I know how you can find out. Using the traceback module you can
get a stack trace each time your function is called from that you can see
where in your code the 2 calls come from.

Add the following to add_annotation.

def add_annotation(self, data_array, fig):
    import traceback
    traceback.print_stack()
1 Like

Thanks @barry-scott when I use the traceback, here is the output in my console

Please don’t post screenshots.

The output would be clearer if you wrapped the output of print_stack() with some kind of marker:

print('*** BEGIN STACK ***')
traceback.print_stack()
print('*** BEGIN STACK ***')

From the output you provided I can see:

  1. run calls process_files … which calls add_annotation.

  2. run calls process_folder which calls process_files … which calls add_annotation.