cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
aadecarlojr
Level III

JMP Python Script editor

In the JMP python script editor, what is the code, after 'import jmp' that points to the open jmp table I want to read and write to with code?  I thought it would be simple and 'native' to JMP python.  I get 'no open table'

1 ACCEPTED SOLUTION

Accepted Solutions
aadecarlojr
Level III

Re: JMP Python Script editor

Thanks to those who responded to help.  I did get it to 'open' the table I wanted.  For those who might be able to use what I came up with to open the jmp table, then export column entries to a third-party web site to extract data from the pages related to each column entry, here is a general form of the Python code I worked out.  I am still working on getting the output to load directly back into the jmp table rather than extracting it from the log, although such log extraction does allow for editing.

 

import jmp
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

# Set up Selenium WebDriver
options = Options()
options.headless = False # Runs Safari in visible mode for debugging

# Initialize the WebDriver (Safari)
driver = webdriver.Safari()

# Path to the JMP file (adjusted to the path you provided)
file_path = "/YOUR JMP PATH.jmp"

# Open the specific table by file path
dt = jmp.open(file_path) # Open the table directly from the file

# Access the 'YOUR_COLUMN' column from the open table
YOUR COLUMN_column = dt["YOUR COLUMN"] # Correct syntax for accessing a column

# Initialize a list to store the results (YOUR NEW COLUMNS)

results = []

# Function to extract YOUR DATA:
url = f'https://www.YOUR WEB PAGE TO EXTRACT FROM'

try:
# Open the URL
driver.get(url)

# Set a short timeout (e.g., 5 seconds) to wait for the page to load
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//a[contains(@href, 'YOUR_KEY')]")))

# Adjust XPath to grab the correct link (e.g., get the first match)
YOUR_tag = driver.find_element(By.XPATH, "//a[contains(@href, 'YOUR_KEY') and not(contains(@href, 'access'))]") # Exclude 'access' related links
YOUR_KEY = YOUR_TAG.text.strip() # Extract the text of YOUR KEY


return YOUR_OUTPUT
except TimeoutException:
print(f"Timeout error for YOUR_COLUMN {YOUR_COLUMN}: URL did not load within the specified time.")
return None, None # If the URL doesn't load, return None YOUR TAGS
except Exception as e:
print(f"Error extracting YOUR TAGS for YOUR_COLUMN {YOUR_COLUMN}: {e}")
return None, None

# Iterate over each row
YOUR_OUTPUT = get_YOUR_OUTPUT(YOUR_COLUMN)
results.append((YOUR_OUTPUT)) # Store the result as a tuple

# Print the results

print(f"YOUR_COLUMN: {YOUR_COLUMN} -> YOUR_TAG: {YOUR_NEW_DATA})

# Close the WebDriver after processing all the entries
driver.quit()

View solution in original post

4 REPLIES 4
jthi
Super User

Re: JMP Python Script editor

Scripting Index and Scripting Guide do have examples for this

jthi_1-1754020831296.png

import jmp

jmp.open(jmp.SAMPLE_DATA + 'Big Class.jmp')
print(jmp.current())

Scripting Guide > Python > Working with JMP Data Tables > Using Operators to Test JMP Objects (not the best example)

Scripting Guide > Python > Python Syntax Reference > jmp Package Functions 

-Jarmo

Re: JMP Python Script editor

The import jmp package is the interface layer between the Python scripts and JMP internals.  An entire Python category has been added to the Scripting Index.  This section provides documentation and examples for interacting with JMP from Python.  The Python Integration is a continuing work in progress.  JMP 18 focuses primarily on the minimum necessary set of capabilities.  Package installation, Data Table interaction, transfer of data between JMP and Python.  JMP 19 expands on this with additional data table column support, better interaction with data frames, additional JMP -> Python, Python -> JMP data type support, and other new capabilities... 

aadecarlojr
Level III

Re: JMP Python Script editor

Thanks to those who responded to help.  I did get it to 'open' the table I wanted.  For those who might be able to use what I came up with to open the jmp table, then export column entries to a third-party web site to extract data from the pages related to each column entry, here is a general form of the Python code I worked out.  I am still working on getting the output to load directly back into the jmp table rather than extracting it from the log, although such log extraction does allow for editing.

 

import jmp
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

# Set up Selenium WebDriver
options = Options()
options.headless = False # Runs Safari in visible mode for debugging

# Initialize the WebDriver (Safari)
driver = webdriver.Safari()

# Path to the JMP file (adjusted to the path you provided)
file_path = "/YOUR JMP PATH.jmp"

# Open the specific table by file path
dt = jmp.open(file_path) # Open the table directly from the file

# Access the 'YOUR_COLUMN' column from the open table
YOUR COLUMN_column = dt["YOUR COLUMN"] # Correct syntax for accessing a column

# Initialize a list to store the results (YOUR NEW COLUMNS)

results = []

# Function to extract YOUR DATA:
url = f'https://www.YOUR WEB PAGE TO EXTRACT FROM'

try:
# Open the URL
driver.get(url)

# Set a short timeout (e.g., 5 seconds) to wait for the page to load
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//a[contains(@href, 'YOUR_KEY')]")))

# Adjust XPath to grab the correct link (e.g., get the first match)
YOUR_tag = driver.find_element(By.XPATH, "//a[contains(@href, 'YOUR_KEY') and not(contains(@href, 'access'))]") # Exclude 'access' related links
YOUR_KEY = YOUR_TAG.text.strip() # Extract the text of YOUR KEY


return YOUR_OUTPUT
except TimeoutException:
print(f"Timeout error for YOUR_COLUMN {YOUR_COLUMN}: URL did not load within the specified time.")
return None, None # If the URL doesn't load, return None YOUR TAGS
except Exception as e:
print(f"Error extracting YOUR TAGS for YOUR_COLUMN {YOUR_COLUMN}: {e}")
return None, None

# Iterate over each row
YOUR_OUTPUT = get_YOUR_OUTPUT(YOUR_COLUMN)
results.append((YOUR_OUTPUT)) # Store the result as a tuple

# Print the results

print(f"YOUR_COLUMN: {YOUR_COLUMN} -> YOUR_TAG: {YOUR_NEW_DATA})

# Close the WebDriver after processing all the entries
driver.quit()

Re: JMP Python Script editor

You have the results, there are multiple ways to put that directly into a data table.  

 

To set a specific cell value in a table

 

dt['col_name'][ row# ] = value

Remember in Python indexing is zero-based, where in JSL it is one-based. So the first row from Python is dt['col'][0]

 

Or set an entire column at a time from a list

 

 

dt['col'] = results

With the above the length of results must match the data tables number of rows,   len(results) == dt.nrows

Also,  dt's data type must match that of the elements of the list, which must all be of the same type.

 

New data tables can be created from Python, as can new columns and rows.

 

 

 

Recommended Articles