You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@toree.apache.org by lr...@apache.org on 2018/09/25 02:50:29 UTC

incubator-toree git commit: [TOREE-483] Add %showOutput magic to disable console output

Repository: incubator-toree
Updated Branches:
  refs/heads/master 7b444ee44 -> 5e7e29ddc


[TOREE-483] Add %showOutput magic to disable console output

Closes #161


Project: http://git-wip-us.apache.org/repos/asf/incubator-toree/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-toree/commit/5e7e29dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-toree/tree/5e7e29dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-toree/diff/5e7e29dd

Branch: refs/heads/master
Commit: 5e7e29ddcfe77e7152c04b0c978b398d744f5d8a
Parents: 7b444ee
Author: Luciano Resende <lr...@apache.org>
Authored: Mon Sep 24 22:49:08 2018 -0400
Committer: Luciano Resende <lr...@apache.org>
Committed: Mon Sep 24 22:49:08 2018 -0400

----------------------------------------------------------------------
 .../apache/toree/kernel/api/KernelOptions.scala |  1 +
 .../apache/toree/magic/builtin/ShowOutput.scala | 40 ++++++++++++++++++++
 .../interpreter/scala/ScalaInterpreter.scala    |  7 ++--
 3 files changed, 45 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/5e7e29dd/kernel-api/src/main/scala/org/apache/toree/kernel/api/KernelOptions.scala
----------------------------------------------------------------------
diff --git a/kernel-api/src/main/scala/org/apache/toree/kernel/api/KernelOptions.scala b/kernel-api/src/main/scala/org/apache/toree/kernel/api/KernelOptions.scala
index 625c14e..194fd5e 100644
--- a/kernel-api/src/main/scala/org/apache/toree/kernel/api/KernelOptions.scala
+++ b/kernel-api/src/main/scala/org/apache/toree/kernel/api/KernelOptions.scala
@@ -20,4 +20,5 @@ package org.apache.toree.kernel.api
 object KernelOptions {
   var showTypes: Boolean = false
   var noTruncation: Boolean = false
+  var showOutput: Boolean = true
 }

http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/5e7e29dd/kernel/src/main/scala/org/apache/toree/magic/builtin/ShowOutput.scala
----------------------------------------------------------------------
diff --git a/kernel/src/main/scala/org/apache/toree/magic/builtin/ShowOutput.scala b/kernel/src/main/scala/org/apache/toree/magic/builtin/ShowOutput.scala
new file mode 100644
index 0000000..8458eb7
--- /dev/null
+++ b/kernel/src/main/scala/org/apache/toree/magic/builtin/ShowOutput.scala
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+package org.apache.toree.magic.builtin
+import java.io.PrintStream
+import org.apache.toree.kernel.api.KernelOptions
+import org.apache.toree.magic.LineMagic
+import org.apache.toree.magic.dependencies.IncludeOutputStream
+import org.apache.toree.plugins.annotations.Event
+class ShowOutput extends LineMagic with IncludeOutputStream {
+  private def printStream = new PrintStream(outputStream)
+  @Event(name = "showoutput")
+  override def execute(code: String): Unit = {
+    code match {
+      case "on" =>
+        printStream.println(s"Console output WILL be shown.")
+        KernelOptions.showOutput = true
+      case "off" =>
+        printStream.println(s"Console output will NOT be shown.")
+        KernelOptions.showOutput = false
+      case "" =>
+        printStream.println(s"Console output display is currently ${if (KernelOptions.showOutput) "on" else "off"}.")
+      case other =>
+        printStream.println(s"${other} is not a valid option for the ShowOutput magic.")
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/5e7e29dd/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala
----------------------------------------------------------------------
diff --git a/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala b/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala
index 66f4d66..86bcc62 100644
--- a/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala
+++ b/scala-interpreter/src/main/scala/org/apache/toree/kernel/interpreter/scala/ScalaInterpreter.scala
@@ -190,6 +190,7 @@ class ScalaInterpreter(private val config:Config = ConfigFactory.load) extends I
    }
 
   def prepareResult(interpreterOutput: String,
+                    showOutput: Boolean = true,
                     showType: Boolean = false,
                     noTruncate: Boolean = false
                    ): (Option[AnyRef], Option[String], Option[String]) = {
@@ -257,8 +258,8 @@ class ScalaInterpreter(private val config:Config = ConfigFactory.load) extends I
     }
 
     (lastResult,
-     if (definitions.nonEmpty) Some(definitions.toString) else None,
-     if (text.nonEmpty) Some(text.toString) else None)
+     if (definitions.nonEmpty && showOutput) Some(definitions.toString) else None,
+     if (text.nonEmpty && showOutput) Some(text.toString) else None)
   }
 
   protected def interpretBlock(code: String, silent: Boolean = false):
@@ -299,7 +300,7 @@ class ScalaInterpreter(private val config:Config = ConfigFactory.load) extends I
          val lastOutput = lastResultOut.toString("UTF-8").trim
          lastResultOut.reset()
 
-         val (obj, defStr, text) = prepareResult(lastOutput, KernelOptions.showTypes, KernelOptions.noTruncation )
+         val (obj, defStr, text) = prepareResult(lastOutput, KernelOptions.showOutput, KernelOptions.showTypes, KernelOptions.noTruncation )
          defStr.foreach(kernel.display.content(MIMEType.PlainText, _))
          text.foreach(kernel.display.content(MIMEType.PlainText, _))
          val output = obj.map(Displayers.display(_).asScala.toMap).getOrElse(Map.empty)