-
Notifications
You must be signed in to change notification settings - Fork 339
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
load snippets location from config #3462
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@bennyweise is attempting to deploy a commit to the marimo Team on Vercel. A member of the Team first needs to authorize it. |
All contributors have signed the CLA ✍️ ✅ |
for more information, see https://pre-commit.ci
marimo/_config/config.py
Outdated
- `custom_path`: the path to the custom snippets directory | ||
""" | ||
|
||
custom_path: NotRequired[str] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts about this being a list of strings? I could see someone asking for multiple paths
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was planning to ask you the same question - I think a list makes more sense here. Also, should the default path optional as well in the config? Something like
[tools.marimo.snippets]
use_default_path: true
custom_paths:
- foo/bar
- foo/baz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea thats not a bad idea. maybe include_default_snippets
with default true
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated no as a list of strings, with include_default_snippets
as an option with default true
marimo/_snippets/snippets.py
Outdated
for _root, _dirs, files in os.walk(custom_root): | ||
for file in files: | ||
if file.endswith(".py"): | ||
yield os.path.join(custom_root, file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this could be DRYed up a bit:
def _yield_py_files(root_path: str) -> Iterator[str]:
real_path = os.path.realpath(root_path)
for _root, _dirs, files in os.walk(real_path):
for file in files:
if file.endswith(".py"):
yield os.path.join(real_path, file)
# Get all paths
default_path = str(import_files("marimo").joinpath("_snippets").joinpath("data"))
config = get_default_config_manager(current_path=None).get_config()
custom_paths: list[str] = config.get("snippets", {}).get("custom_paths", [])
if custom_paths:
LOGGER.debug("Using custom snippets path: %s", custom_paths)
paths = [default_path] + custom_paths
for path in paths:
yield from _yield_py_files(path)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be more performant / easier to read using pathlib
# Get all paths
default_path = import_files("marimo") / "_snippets" / "data"
config = get_default_config_manager(current_path=None).get_config()
custom_paths = [Path(p) for p in config.get("snippets", {}).get("custom_paths", [])]
paths = [default_path] + custom_paths
# Yield from all paths
for root_path in paths:
# TODO: maybe error catching if the path is not valid?
for file in root_path.resolve().rglob("*.py"):
yield str(file)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically conforms to this style now - thanks for the pointers
Have updated based on the comments, and tested locally with the standard test cases of real directory / non-existent directory / recursively symlinked directory, and all look ok (but I may be missing some pathological cases). In terms of formal tests, I was wondering if there is a recommended way to test with different configs - I could patch the |
I have read the CLA Document and I hereby sign the CLA |
hey @bennyweise, this looks great! I do think a test would be nice. You don't need to test change the config, etc. but maybe we can refactor the
and we can write a few different tests for
|
📝 Summary
🔍 Description of Changes
📋 Checklist
📜 Reviewers
@akshayka OR @mscolnick