Skip to content

R notebooks: add py_print() helper and document Jupyter output limitations#492

Merged
cachafla merged 12 commits intomainfrom
cachafla/sc-15256/notebook-improvements
Mar 25, 2026
Merged

R notebooks: add py_print() helper and document Jupyter output limitations#492
cachafla merged 12 commits intomainfrom
cachafla/sc-15256/notebook-improvements

Conversation

@cachafla
Copy link
Copy Markdown
Contributor

@cachafla cachafla commented Mar 25, 2026

Pull Request Description

What and why?

image

Follow-up to #490. When running R notebooks in Jupyter (via IRkernel), Python print() output is not displayed automatically — this is a reticulate limitation where Python's stdout doesn't route to the R kernel's display system.

This PR adds a py_print() helper to the R package that captures Python print output and displays it via R's cat(). It works for functions that use print() (like preview_template(), preview_test_config()). Python logging output (from run_documentation_tests(), assign_predictions()) cannot be captured due to how reticulate manages stdout between calls — these run silently and users check the ValidMind Platform for results.

Changes:

  • Add py_print() function to r/validmind/R/platform.R using reticulate::py_capture_output() with NSE
  • Export py_print in NAMESPACE
  • Wrap preview_template() and preview_test_config() calls in notebooks with py_print()
  • Document the helper and Jupyter output limitations in the R README

How to test

  1. Install the R package: install.packages("r/validmind", repos = NULL, type = "source")
  2. Open notebooks/quickstart/quickstart_model_documentation.Rmd in Jupyter with an R kernel
  3. Run the py_print(vm_r$preview_template()) cell — should display the template tree
  4. Verify run_documentation_tests() runs without error (output is silent in Jupyter, check the platform)

What needs special review?

  • The py_print() NSE implementation (substitute + eval in parent.frame())
  • Whether this is the right level of documentation for the Jupyter limitation

Dependencies, breaking changes, and deployment notes

Depends on #490 being merged (already merged). No breaking changes.

Release notes

Added py_print() helper to the R package for displaying Python print output in R Jupyter notebooks, and documented known limitations with Python logging output in the Jupyter environment.

Checklist

  • What and why
  • Screenshots or videos (Frontend)
  • How to test
  • What needs special review
  • Dependencies, breaking changes, and deployment notes
  • Labels applied
  • PR linked to Shortcut
  • Unit tests added (Backend)
  • Tested locally
  • Documentation updated (if required)
  • Environment variable additions/changes documented (if required)

🤖 Generated with Claude Code

@cachafla cachafla added the internal Not to be externalized in the release notes label Mar 25, 2026
@cachafla cachafla requested review from nrichers and validbeck March 25, 2026 22:32
cachafla and others added 2 commits March 25, 2026 15:35
- Generate py_print.Rd (was missing)
- Update vm.Rd to reflect document param and python_version default

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@nrichers nrichers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Squints ... LGTM! :shipit:

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

PR Summary

This PR introduces a new helper function, py_print, to enhance the interaction between Python and R in environments such as R Jupyter notebooks. In typical scenarios using reticulate, Python's print() output may not be correctly captured in R; this update wraps Python expressions with reticulate::py_capture_output() and uses cat() to explicitly print the output. The changes include:

  • Modification of several R markdown and R documentation files to replace direct calls like vm_r$preview_template() with py_print(vm_r$preview_template()), ensuring that Python output is visible in R notebook sessions.
  • Export and proper documentation for the newly added py_print function in the package's NAMESPACE and roxygen-generated documentation.
  • Additional guidance in the README on using py_print for displaying Python output in Jupyter notebooks, while noting its limitations (e.g., inability to capture Python logging output).
  • Minor updates to the R package documentation to reflect the functional changes in the vm() function, especially the handling of Python output.

These enhancements aim to improve usability for users running R in hybrid Python environments by providing a consistent mechanism for capturing and displaying Python print() outputs.

Test Suggestions

  • Create a small R notebook that calls py_print on a Python function that produces output and verify that the output is correctly printed to the console.
  • Test the behavior in both terminal R sessions and R Jupyter notebooks to ensure consistent output handling.
  • Verify that wrapping expressions with py_print still returns the expected output invisibly for further programmatic use.
  • Run any unit tests for the R package to check that the new function does not break existing functionality.

@cachafla cachafla merged commit 82765d9 into main Mar 25, 2026
20 checks passed
@cachafla cachafla deleted the cachafla/sc-15256/notebook-improvements branch March 25, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Not to be externalized in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants