Skip to content

feat: improve Python notebook cell output (#1505)#1857

Open
stu-code wants to merge 4 commits intosassoftware:mainfrom
stu-code:feature-1515
Open

feat: improve Python notebook cell output (#1505)#1857
stu-code wants to merge 4 commits intosassoftware:mainfrom
stu-code:feature-1515

Conversation

@stu-code
Copy link
Copy Markdown

@stu-code stu-code commented Apr 8, 2026

Summary:

Improvements to Python code blocks based on feature request 1505:

  • Suppress SAS log for Python cells; show output only when there is Python stdout, a Python error, or a SAS-level error:
image image image image
  • Render Python exceptions using VS Code's built-in error renderer with coloring and highlighting
image
  • Python code that fails to run results in an X on the notebook cell

  • New default - Auto-display the last expression in a Python cell: objects with repr_html() (DataFrames, sklearn models, pipelines) are rendered as rich HTML via base64 extraction; all other values fall back to print(). A second optional mode, SAS.show(), renders via ODS HTML5 and is configurable via SAS.notebook.python.autoPrintMethod. Configuration is available in UI.

image image image image

Testing:
Tests were performed manually in a Python code block with the exception of (4) which uses a SAS code block to create a SAS error condition. (See TODOs for updating build tests)

1. Tests for if there is no Python stdout or an error:

Test 1.1:

import pandas as pd

Expected result: No output

Test 1.2:

foo = 'bar'

Expected result: No output

Test 1.3:

SAS.symput('foo', 'bar')

Expected result: No output


2. Tests for Python output:

Test 2.1:

40+2

Expected result:

42

Test 2.2:

{'Hello': 'world'}

Expected result:

{'Hello': 'world'}

Test 2.3:

asdf

Expected result:

--------------------------------------------------------------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)
  File "<stdin>", line 5, in <module>
  File "<stdin>", line 2, in <module>
  File "<string>", line 1, in <module>
NameError: name 'asdf' is not defined

3. Test DataFrame and model rendering.

Test 3.1:

import pandas as pd
df = pd.DataFrame({'foo': [1, 2, 3], 'bar': [4, 5, 6]})
df
from sklearn.ensemble import GradientBoostingClassifier
model = GradientBoostingClassifier()
model

Test 3.2: Switch to SAS.show from (auto) in SAS extension Python autoprint settings:

import pandas as pd
df = pd.DataFrame({'foo': [1, 2, 3], 'bar': [4, 5, 6]})
df
from sklearn.ensemble import GradientBoostingClassifier
model = GradientBoostingClassifier()
model

Expected result: DataFrame renders in ODS HTML5. Model produces the following:

This method doesn't know the object passed in. This is an object of type <class 'sklearn.ensemble._gb.GradientBoostingClassifier'>.
GradientBoostingClassifier()

4. Test SAS errors: run the following SAS code, then run a Python code block:

Test 4.1:

options set=PROC_PYPATH="/foo";

proc python restart;
run;
'Hello world!'

Expected result:

ERROR: Failed to create Python subprocess.
ERROR: Launch Failed. Cannot execute program "/foo", error = 2 (No such file or directory).
ERROR: The specified executable module either cannot be located or it is not a valid executable.
ERROR: Failed to launch Python sub-process in tkpy extension.

TODOs:

  • Update build test cases for new expected Python output behavior
  • Translate new Python autoprint option to other languages

Stu Sztukowski added 2 commits April 7, 2026 21:09
- Suppress SAS log for Python cells; show output only when
  there is Python stdout, a Python error, or a SAS-level error

- Python code that fails to run results in an X on the notebook cell

- Render Python exceptions using VS Code's built-in error renderer with
  coloring and highlighting

- New default - Auto-display the last expression in a Python cell:
  objects with _repr_html_() (DataFrames, sklearn models, pipelines)
  are rendered as rich HTML via base64 extraction; all other values
  fall back to print(). A second optional mode, SAS.show(), renders via ODS
  HTML5 and is configurable via SAS.notebook.python.autoPrintMethod. Configuration
  is available in UI.

Signed-off-by: Stu Sztukowski <stu.sztukowski@sas.com>
Signed-off-by: Stu Sztukowski <stu.sztukowski@sas.com>
@stu-code
Copy link
Copy Markdown
Author

stu-code commented Apr 8, 2026

Note that in order for this to pass the build test, it will need to be updated for the new Python code block output behavior.

@Wizzzzzzard Wizzzzzzard requested review from 2TomLi, scottdover and smorrisj and removed request for scottdover April 8, 2026 07:54
Signed-off-by: Stu Sztukowski <stu.sztukowski@sas.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant