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

Format jupyter notebooks #7598

Closed
konstin opened this issue Sep 22, 2023 · 2 comments · Fixed by #7749
Closed

Format jupyter notebooks #7598

konstin opened this issue Sep 22, 2023 · 2 comments · Fixed by #7749
Assignees
Labels
formatter Related to the formatter help wanted Contributions especially welcome

Comments

@konstin
Copy link
Member

konstin commented Sep 22, 2023

We currently lint and fix python files and jupyter notebook but only format python files.

Formatting jupyter notebooks can reuse the infrastructure we use to lint and fix them, it should mostly be a matter of formatting each cell in a loop (being mindful of leading and trailing newlines) and adding a test.

@konstin konstin added formatter Related to the formatter good first issue Good for newcomers labels Sep 22, 2023
@MichaReiser MichaReiser added help wanted Contributions especially welcome and removed good first issue Good for newcomers labels Sep 27, 2023
@charliermarsh charliermarsh added this to the Formatter: Beta milestone Oct 1, 2023
@charliermarsh
Copy link
Member

This isn't required for the Beta but it'd be nice to have for the release announcement and such.

@charliermarsh charliermarsh self-assigned this Oct 1, 2023
@charliermarsh
Copy link
Member

I have a working version of this going.

charliermarsh added a commit that referenced this issue Oct 2, 2023
## Summary

This PR enables `ruff format` to format Jupyter notebooks.

Most of the work is contained in a new `format_source` method that
formats a generic `SourceKind`, then returns `Some(transformed)` if the
source required formatting, or `None` otherwise.

Closes #7598.

## Test Plan

Ran `cat foo.py | cargo run -p ruff_cli -- format --stdin-filename
Untitled.ipynb`; verified that the console showed a reasonable error:

```console
warning: Failed to read notebook Untitled.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: EOF while parsing a value at line 1 column 0
```

Ran `cat Untitled.ipynb | cargo run -p ruff_cli -- format
--stdin-filename Untitled.ipynb`; verified that the JSON output
contained formatted source code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants