mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-29 16:03:50 +00:00
Write full Jupyter notebook to stdout
(#7748)
## Summary When writing back notebooks via `stdout`, we need to write back the entire JSON content, not _just_ the fixed source code. Otherwise, writing the output _back_ to the file will yield an invalid notebook. Closes https://github.com/astral-sh/ruff/issues/7747 ## Test Plan `cargo test`
This commit is contained in:
parent
c71ff7eae1
commit
ebdfcee87f
4 changed files with 190 additions and 6 deletions
|
@ -414,11 +414,13 @@ impl Notebook {
|
|||
}
|
||||
|
||||
/// Write the notebook back to the given [`Write`] implementor.
|
||||
pub fn write(&self, writer: &mut dyn Write) -> anyhow::Result<()> {
|
||||
pub fn write(&self, writer: &mut dyn Write) -> Result<(), NotebookError> {
|
||||
// https://github.com/psf/black/blob/69ca0a4c7a365c5f5eea519a90980bab72cab764/src/black/__init__.py#LL1041
|
||||
let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
|
||||
let mut serializer = serde_json::Serializer::with_formatter(writer, formatter);
|
||||
SortAlphabetically(&self.raw).serialize(&mut serializer)?;
|
||||
SortAlphabetically(&self.raw)
|
||||
.serialize(&mut serializer)
|
||||
.map_err(NotebookError::Json)?;
|
||||
if self.trailing_newline {
|
||||
writeln!(serializer.into_inner())?;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue