diff --git a/.gitignore b/.gitignore index e0b228588a829f886c664428704937c077f140bf..89a2efa3b17e58220c81c868e1e1ca965d04d96f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /target **/*.rs.bk *.json + +.idea diff --git a/src/input_types/ipynb.rs b/src/input_types/ipynb.rs index 0b6bf383bfd56aa8aaeeb04df7fb6f1fa2b2c370..9cdd4f71943e27b9ac4f458555025281b38cb98f 100644 --- a/src/input_types/ipynb.rs +++ b/src/input_types/ipynb.rs @@ -158,6 +158,8 @@ pub struct CodeCell { pub execution_count: Option<i64>, } +const MAX_OUTPUT_LINES: usize = 50; + impl Display for CodeCell { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { let exec_count_as_str = self @@ -168,8 +170,33 @@ impl Display for CodeCell { writeln!(f, "{}\n", self.source)?; writeln!(f, "# Out[{}]:\n", exec_count_as_str)?; + + let mut has_stream_output = false; for output in &self.outputs { - writeln!(f, "{}", comment_out(output.to_string()))?; + let mut out_str = output.to_string(); + + // IPython batches stream outputs if they are too long + // we only want to show the first batch of stream outputs. + // Also, we want to truncate too long output streams. + if let Output::Stream(stream) = output { + if has_stream_output { + continue; + } + has_stream_output = true; + + out_str = stream + .to_string() + .lines() + .take(MAX_OUTPUT_LINES) + .collect::<Vec<_>>() + .join("\n"); + + if out_str.lines().count() == MAX_OUTPUT_LINES { + out_str.push_str("\nOUTPUT HAS BEEN TRUNCATED. SEE ORIGINAL NOTEBOOK FOR FULL OUTPUT."); + } + } + + writeln!(f, "{}", comment_out(out_str))?; } Ok(()) } diff --git a/src/transform/submissions.rs b/src/transform/submissions.rs index 1d34614a8c1e4f86d992037a3a8c3f9894e3ada1..f9c11349ba7940c1d9c1083f4b8385bceefedee8 100644 --- a/src/transform/submissions.rs +++ b/src/transform/submissions.rs @@ -49,7 +49,7 @@ pub fn transform_submissions( tests: vec![], }; - if sub_type.programming_language == ProgrammingLang::Python { + if sub_type.programming_language == ProgrammingLang::Python && submission.code.len() > 0 { render_code::<Notebook>(&mut submission)?; } Ok(submission)