Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD] Use set_view() in builtin view operations #4960

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

brimoor
Copy link
Contributor

@brimoor brimoor commented Oct 22, 2024

Updates the builtin save_view, edit_saved_view_info, and delete_saved_view operators as follows:

  • save_view: when saving the current view, use ctx.ops.set_view() to update the App to reflect the fact that the current view is now saved
  • edit_saved_view_info: when renaming the current view, use ctx.ops.set_view() to update the App to reflect the current view's new name
  • delete_saved_view: when deleting the current view, reset the App to the full dataset

This is consistent with how save_workspace, edit_workspace_info, and delete_workspace work as of #4902.

Summary by CodeRabbit

  • New Features

    • Enhanced flexibility in saving views with a new parameter for the SaveView operator.
    • Improved management of saved view information with the EditSavedViewInfo operator.
    • Added functionality to reset the context view after deleting a saved view.
    • Updated LoadWorkspace and SaveWorkspace operators for better workspace context handling.
    • Introduced utility functions for parsing views and spaces.
  • Bug Fixes

    • Refined error handling for clearer messages during operations.

@brimoor brimoor requested a review from imanjra October 22, 2024 02:56
Copy link
Contributor

coderabbitai bot commented Oct 22, 2024

Walkthrough

The changes in this pull request enhance the functionality of the fiftyone/operators/builtin.py file by modifying several operators related to views and workspaces. Key updates include the addition of a view parameter in the SaveView operator, adjustments to the EditSavedViewInfo and DeleteSavedView operators for better context management, and improvements to the LoadWorkspace and SaveWorkspace operators. Additionally, two new utility functions are introduced, and error handling is refined to improve clarity.

Changes

File Change Summary
fiftyone/operators/builtin.py - Updated SaveView, EditSavedViewInfo, DeleteSavedView, LoadWorkspace, and SaveWorkspace operators to handle new parameters for views and workspaces.
- Added utility functions _parse_view and _parse_spaces.
- Refined error handling for various operators.

Possibly related PRs

  • fix save workspace built-in operator #4868: This PR modifies the fiftyone/operators/builtin.py file, specifically enhancing the SaveWorkspace operator, which is directly related to the changes made in the main PR that also involves modifications to operators in the same file.

Suggested reviewers

  • ritch
  • benjaminpkane

🐰 In the garden where views grow,
Operators dance, putting on a show.
With spaces saved and names anew,
Flexibility blooms, as we hop and chew!
Error messages clear, like a sunny day,
Let's celebrate changes, hip-hip-hooray! 🌼


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.

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.

@brimoor
Copy link
Contributor Author

brimoor commented Oct 22, 2024

@imanjra note in the attached video that ctx.ops.set_view(name=name) doesn't immediately work when name is a newly created view. You must refresh the App in order for it to recognize the new view.

Interestingly, the exactly analogous steps work for ctx.ops.set_spaces(name=name) introduced in #4902 does work.

Is it an easy fix to tweak the App so that new saved views can be immediately referenced?

save_view_issues.mov

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c21cc6d and 36a1959.

📒 Files selected for processing (1)
  • fiftyone/operators/builtin.py (5 hunks)
🧰 Additional context used
🪛 Ruff
fiftyone/operators/builtin.py

1696-1699: Use ternary operator view = ctx.view if curr_view else _parse_view(ctx, view) instead of if-else-block

Replace if-else-block with view = ctx.view if curr_view else _parse_view(ctx, view)

(SIM108)

🔇 Additional comments (2)
fiftyone/operators/builtin.py (2)

1737-1744: Ensure current view is updated after renaming

The code correctly updates the current view if its name has changed, ensuring consistency within the application.


1855-1860: Reset current view after deleting the active saved view

The implementation properly resets the current view to the dataset's default view when the active saved view is deleted.

Comment on lines +2310 to +2313
if isinstance(view, str):
view = json.loads(view)

return fo.DatasetView._build(ctx.dataset, view)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for JSON parsing in _parse_view

Currently, the _parse_view function does not handle exceptions that might occur during JSON parsing. If view contains invalid JSON, json.loads(view) will raise a json.JSONDecodeError, which is not caught. Consider adding error handling to manage this exception and provide a meaningful error message.

Apply this diff to handle JSON parsing errors:

 def _parse_view(ctx, view):
     if isinstance(view, str):
-        view = json.loads(view)
+        try:
+            view = json.loads(view)
+        except json.JSONDecodeError as e:
+            raise ValueError(f"Invalid JSON for view: {e}")
     return fo.DatasetView._build(ctx.dataset, view)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if isinstance(view, str):
view = json.loads(view)
return fo.DatasetView._build(ctx.dataset, view)
if isinstance(view, str):
try:
view = json.loads(view)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON for view: {e}")
return fo.DatasetView._build(ctx.dataset, view)

Comment on lines +1696 to +1699
if curr_view:
view = ctx.view
else:
view = _parse_view(ctx, view)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Simplify conditional assignment using a ternary operator

Consider simplifying the assignment of view by using a ternary operator for conciseness.

Apply this diff to streamline the code:

 if curr_view:
     view = ctx.view
 else:
     view = _parse_view(ctx, view)
+view = ctx.view if curr_view else _parse_view(ctx, view)
-

Committable suggestion was skipped due to low confidence.

🧰 Tools
🪛 Ruff

1696-1699: Use ternary operator view = ctx.view if curr_view else _parse_view(ctx, view) instead of if-else-block

Replace if-else-block with view = ctx.view if curr_view else _parse_view(ctx, view)

(SIM108)

@imanjra
Copy link
Contributor

imanjra commented Oct 22, 2024

@brimoor yah, I believe it may require slight refactor. For workspace, I think it does a trip to backend so it can get latest workspaces but for view it does it all in the frontend. I'll check it out. It should be doable.

Copy link
Contributor

@imanjra imanjra left a comment

Choose a reason for hiding this comment

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

LGTM

description=description,
color=color,
overwrite=True,
)

if curr_view:
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of scope of this PR as it matches workspaces, but we should allow opting out of this for programmatic execution

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm good point, is there a way to tell if an operator is being executed programmatically vs prompting?

ctx.dataset.update_saved_view_info(name, info)

if curr_name is not None and curr_name != new_name:
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

ctx.dataset.delete_saved_view(name)

if curr_view:
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

@brimoor brimoor changed the title Use set_view() in builtin view operations [HOLD] Use set_view() in builtin view operations Oct 22, 2024
@brimoor brimoor marked this pull request as draft October 22, 2024 14:44
@brimoor
Copy link
Contributor Author

brimoor commented Oct 22, 2024

Just to clarify, we can't merge this until/unless saved views are updated per this comment. As it currently stands, one gets the error notifications pictured in the video there.

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