cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar

Running JSL within a Python script behaves differently if included in a python function such as main()

Version: JMP 18.0.1

 

I have included 2 python scripts below. Both have the creation of a figure wrapped inside a function and saved as an image. If I call the image file directly using Python Get () I am able to use the jmp.run_jsl to open a window with the chart. However, if I do the same thing but wrap the jmp.run_jsl within a main() function for some reason the Python Get() does not work and I am not able to pass the string for the file location into the new picture window. Any help would be greatly appreciated.

1 REPLY 1
Eki
Eki
New Member

Re: Running JSL within a Python script behaves differently if included in a python function such as main()

Hi!


I guess it has to do with variable scopes. In testcode_v1.txt plot variable is defined inside the function main() whereas in testcode_v2.txt plot variable is defined outside of any limiting scopes, making it global. Apparently, despite executing JSL script inside some inner scope does not mean you have access to the variables defined in that particular scope.

The minimal effort workaround for this is to define plot variable as a global. See the parts highlighted with red.

import matplotlib.pyplot as plt
import jmp

plot = None

def show_plot():
  fig, ax = plt.subplots()

  fruits = ['apple', 'blueberry', 'cherry', 'orange']
  counts = [40, 100, 30, 55]
  bar_labels = ['red', 'blue', '_red', 'orange']
  bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']

  ax.bar(fruits, counts, label=bar_labels, color=bar_colors)

  ax.set_ylabel('fruit supply')
  ax.set_title('Fruit supply by kind and color')
  ax.legend(title='Fruit color')

  plot = "your_file_path.png"
  plt.savefig(plot,format='png')
  return plot


def main():
  global plot
  plot = show_plot()
  print(plot)
  jmp.run_jsl('''
  model = Python Get (plot);
  show (model);
  plot_model = Open(model, png);
  pngJMP = New Window( "Test Model Representation", Picture Box( plot_model ),
    << bring window to front;
    );
  Python Submit( "plt.close()" );
  ''')


if __name__ == "__main__":
  main()