Jump to content

exit a python data function early

David Katz

Recommended Posts

What I tend to do is:

  • create an 'empty_return' function to call if the input is invalid or there is an early termination
  • this function returns exactly the same output parameters as needed, but empty versions of them
  • I add a message output string to the output parameters, so it can be saved in a document property and displayed.

Other very useful tips on Python data functions in Spotfire in this Community article:


Simple example: I put the main code in a function so it could be exited early in different places if needed.

In this case there is an integer input parameter my_parm and three output parameters: two data frames and a message string.

import pandas as pdimport numpy as np def empty_return(msg): df1=pd.DataFrame({'errcol':[np.nan]}) df2=pd.DataFrame({'errcol':[np.nan]}) return (df1,df2,msg) def main(parm): if parm==0: return empty_return('parameter was zero') #otherwise normal execution aa=[1,2,3] bb=['A','B','C'] cc=['a','b','c'] df1=pd.DataFrame({'col1':aa,'col2':bb}) df2=pd.DataFrame({'col1':aa,'col2':cc}) message='all good' return (df1,df2,message) df1,df2,message=main(my_parm) 
Link to comment
Share on other sites

Thanks Gaia.


This is a good solution. I was hesitant to use it because my data function has a long list of parameters, and I was trying to avoid this:




But your endorsement of 'main' led me to consider this - use the tip in the link you provided to load the Spotfire parameters directly into main():


def main(debug):

if debug = True:


main(False) within SF

main(True) to debug in emacs or...


Does this make sense to you?


*David Katz*, TIBCO Data Science




Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...