Skip to content

fix: notebook execution for apptainer#3131

Merged
johanneskoester merged 2 commits intosnakemake:mainfrom
laf070810:fix-apptainer-notebook
Oct 11, 2024
Merged

fix: notebook execution for apptainer#3131
johanneskoester merged 2 commits intosnakemake:mainfrom
laf070810:fix-apptainer-notebook

Conversation

@laf070810
Copy link
Copy Markdown
Contributor

@laf070810 laf070810 commented Oct 11, 2024

At

if is_python_script:
# mount host snakemake module into container
args += " ".join(
f" --bind {repr(searchpath)}:{repr(mountpoint)}"
for searchpath, mountpoint in zip(
get_snakemake_searchpaths(), get_snakemake_searchpath_mountpoints()
)
)

snakemake searchpaths are only bound into apptainer when is_python_script is True. For notebook directive, is_python_script will be False and snakemake searchpaths are not bound into apptainer, causing ModuleNotFoundError: No module named 'snakemake' error in the preamble. This PR sets is_python_script to True for notebook directive.

Also, the name, is_python_script, could then be somewhat confusing. Changing it to something like is_script or has_preamble might be better.

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).

Summary by CodeRabbit

  • New Features

    • Introduced a new parameter to enhance script execution for Python scripts in Jupyter notebooks.
    • Added functionality to determine whether a notebook should be drafted or executed based on user input.
  • Bug Fixes

    • Improved validation for local notebook paths to ensure editing is only allowed for local files.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 11, 2024

Walkthrough

The changes introduce a new parameter, is_python_script, to the _execute_cmd method within the execute_script method of the JupyterNotebook class in snakemake/notebook.py. This parameter is set to True to indicate that the executed script is a Python script. Additionally, the notebook function now checks the edit parameter to determine if a notebook should be drafted or executed, refining logic for local path validation and file extension checks. The overall structure of the classes and methods remains unchanged.

Changes

File Change Summary
snakemake/notebook.py Added is_python_script=True parameter to _execute_cmd call in execute_script. Updated notebook function to check edit parameter and validate local paths.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant JupyterNotebook
    participant CommandExecutor

    User->>JupyterNotebook: execute_script(fname, edit)
    JupyterNotebook->>CommandExecutor: _execute_cmd(fname, is_python_script=True)
    CommandExecutor-->>JupyterNotebook: Execute script
    JupyterNotebook-->>User: Return execution result
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between afa7bad and e74b593.

📒 Files selected for processing (1)
  • snakemake/notebook.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
snakemake/notebook.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

🔇 Additional comments (1)
snakemake/notebook.py (1)

97-97: LGTM! Verify impact on other parts of the codebase.

The addition of is_python_script=True to the _execute_cmd call aligns with the PR objective and should resolve the issue of Snakemake search paths not being integrated into Apptainer for notebook execution. This change is consistent and straightforward.

To ensure this change doesn't have unintended consequences, please run the following verification:

Also, consider updating the documentation to reflect this change in behavior for notebook execution.

✅ Verification successful

Verified! No issues found with the addition of is_python_script=True.

The change in snakemake/notebook.py is consistent with existing implementations and does not introduce any conflicts or unintended side effects.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other occurrences of _execute_cmd and verify if they need similar changes

# Search for other _execute_cmd calls
rg "_execute_cmd\(" --type python

# Search for references to is_python_script
rg "is_python_script" --type python

Length of output: 133


Script:

#!/bin/bash
# Description: Check for other occurrences of _execute_cmd and verify if they need similar changes

# Find all Python files and search for _execute_cmd calls
fd --extension py --type file | xargs rg "_execute_cmd\("

# Find all references to is_python_script in Python files
fd --extension py --type file | xargs rg "is_python_script"

Length of output: 1253


Script:

#!/bin/bash
# Description: Review all usages of is_python_script to ensure consistency and identify if further changes are needed

# Find all assignments of is_python_script
fd --extension py --type file | xargs rg "is_python_script\s*="

# Find all conditional checks of is_python_script
fd --extension py --type file | xargs rg "if\s+is_python_script"

# Find all docstrings or comments mentioning is_python_script
fd --extension py --type file | xargs rg "is_python_script.*(docstring|comment)"

Length of output: 609


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@laf070810
Copy link
Copy Markdown
Contributor Author

Also, it may be better to add some tests for notebook execution in apptainer, to prevent trivial bugs like this one here.

Copy link
Copy Markdown
Contributor

@johanneskoester johanneskoester left a comment

Choose a reason for hiding this comment

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

Good catch! To me the name also sounds correct for a notebook.

@johanneskoester
Copy link
Copy Markdown
Contributor

Also, it may be better to add some tests for notebook execution in apptainer, to prevent trivial bugs like this one here.

Indeed! I won't be able to do that now, but if you want to do it in this PR please feel free to do so. If not, it would be ok to merge this without or while adding a TODO.

@laf070810
Copy link
Copy Markdown
Contributor Author

Also, it may be better to add some tests for notebook execution in apptainer, to prevent trivial bugs like this one here.

Indeed! I won't be able to do that now, but if you want to do it in this PR please feel free to do so. If not, it would be ok to merge this without or while adding a TODO.

Thanks for the quick reply! I would recommend merging this first, as this bug makes notebook in apptainer unusable. I can open an issue to track on adding tests and do it later.

@sonarqubecloud
Copy link
Copy Markdown

@johanneskoester johanneskoester merged commit 2e382c4 into snakemake:main Oct 11, 2024
johanneskoester pushed a commit that referenced this pull request Oct 12, 2024
🤖 I have created a release *beep* *boop*
---


##
[8.21.0](v8.20.7...v8.21.0)
(2024-10-12)


### Features

* support for specifying conda envs as directories
([#3132](#3132))
([c54c95d](c54c95d))


### Bug Fixes

* better error handling in evaluate function
([#3129](#3129))
([04fb97f](04fb97f))
* notebook execution for apptainer
([#3131](#3131))
([2e382c4](2e382c4))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.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.

2 participants