Skip to content

Commit

Permalink
Display Standard Out (#127)
Browse files Browse the repository at this point in the history
Attach a string builder as System.Console.Out and send its contents
to the notebook on Display and when execution completes.

Do not send a "pyout" message when the result of a cell is of type
unit, in order to avoid printing "<null>".
  • Loading branch information
sylvanc committed Jan 19, 2017
1 parent 2e0bf6f commit ee394e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/IfSharp.Kernel/App.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module App =
let Display (value : obj) =

if value <> null then
if sbPrint.Length > 0 then
Kernel.Value.SendDisplayData("text/plain", sbPrint.ToString())
sbPrint.Clear() |> ignore

let printer = Printers.findDisplayPrinter(value.GetType())
let (_, callback) = printer
let callbackValue = callback(value)
Expand Down
8 changes: 6 additions & 2 deletions src/IfSharp.Kernel/Evaluation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ module Evaluation =
let internal startSession () =
let sbOut = new StringBuilder()
let sbErr = new StringBuilder()
let sbPrint = new StringBuilder()
let inStream = new StringReader("")
let outStream = new StringWriter(sbOut)
let errStream = new StringWriter(sbErr)
let printStream = new StringWriter(sbPrint)

let fsiObj = Microsoft.FSharp.Compiler.Interactive.Settings.fsi
let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration(fsiObj, false)
Expand All @@ -54,10 +56,12 @@ module Evaluation =
let extraPrinters =
unbox<ResizeArray<System.Type * (obj -> seq<string * string> * string)>>
(fsiSession.EvalExpression("FsInteractiveService.htmlPrinters").Value.ReflectionValue)
sbErr, sbOut, extraPrinters, fsiSession

Console.SetOut(printStream)
sbErr, sbOut, sbPrint, extraPrinters, fsiSession

let internal fsiout = ref false
let internal sbErr, sbOut, extraPrinters, fsiEval = startSession ()
let internal sbErr, sbOut, sbPrint, extraPrinters, fsiEval = startSession ()

/// Gets `it` only if `it` was printed to the console
let GetLastExpression() =
Expand Down
14 changes: 9 additions & 5 deletions src/IfSharp.Kernel/Kernel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
// clear some state
sbOut.Clear() |> ignore
sbErr.Clear() |> ignore
sbPrint.Clear() |> ignore
payload.Clear()

// only increment if we are not silent
Expand All @@ -323,6 +324,9 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
handleException exn
Some exn

if sbPrint.Length > 0 then
sendDisplayData "text/plain" (sbPrint.ToString()) "display_data"

if sbErr.Length > 0 then
let err = sbErr.ToString().Trim()
let executeReply =
Expand Down Expand Up @@ -353,11 +357,11 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
let lastExpression = GetLastExpression()
match lastExpression with
| Some(it) ->
let printer = Printers.findDisplayPrinter(it.ReflectionType)
let (_, callback) = printer
let callbackValue = callback(it.ReflectionValue)
sendDisplayData callbackValue.ContentType callbackValue.Data "pyout"
if it.ReflectionType <> typeof<unit> then
let printer = Printers.findDisplayPrinter(it.ReflectionType)
let (_, callback) = printer
let callbackValue = callback(it.ReflectionValue)
sendDisplayData callbackValue.ContentType callbackValue.Data "pyout"

| None -> ()

Expand Down

0 comments on commit ee394e8

Please sign in to comment.