Commit aed969c2 authored by Sumit Sahrawat's avatar Sumit Sahrawat

Finalize Output Widget

- Add support for `clear_output`
- Rename `setOutput` to `appendOutput`
parent d7565d15
...@@ -12,7 +12,10 @@ module IHaskell.Display.Widgets.Output ( ...@@ -12,7 +12,10 @@ module IHaskell.Display.Widgets.Output (
modifyOutputWidth, modifyOutputWidth,
modifyOutputWidth_, modifyOutputWidth_,
-- * Output to widget -- * Output to widget
setOutput, appendOutput,
clearOutput,
clearOutput_,
replaceOutput,
) where ) where
-- To keep `cabal repl` happy when running from the ihaskell repo -- To keep `cabal repl` happy when running from the ihaskell repo
...@@ -72,13 +75,31 @@ modifyOutputWidth widget modifier = getOutputWidth widget >>= modifier >>= setOu ...@@ -72,13 +75,31 @@ modifyOutputWidth widget modifier = getOutputWidth widget >>= modifier >>= setOu
-- | Modify the output widget width (with pure modifier) -- | Modify the output widget width (with pure modifier)
modifyOutputWidth_ :: OutputWidget -> (Int -> Int) -> IO () modifyOutputWidth_ :: OutputWidget -> (Int -> Int) -> IO ()
modifyOutputWidth_ widget modifier = getOutputWidth widget >>= setOutputWidth widget . modifier modifyOutputWidth_ widget modifier = do
w <- getOutputWidth widget
setOutput :: IHaskellDisplay a => OutputWidget -> a -> IO () let newWidth = modifier w
setOutput widget out = do setOutputWidth widget newWidth
-- | Append to the output widget
appendOutput :: IHaskellDisplay a => OutputWidget -> a -> IO ()
appendOutput widget out = do
disp <- display out disp <- display out
widgetPublishDisplay widget disp widgetPublishDisplay widget disp
-- | Clear the output widget immediately
clearOutput :: OutputWidget -> IO ()
clearOutput widget = widgetClearOutput widget False
-- | Clear the output widget on next append
clearOutput_ :: OutputWidget -> IO ()
clearOutput_ widget = widgetClearOutput widget True
-- | Replace the currently displayed output for output widget
replaceOutput :: IHaskellDisplay a => OutputWidget -> a -> IO ()
replaceOutput widget d = do
clearOutput_ widget
appendOutput widget d
instance ToJSON OutputWidget where instance ToJSON OutputWidget where
toJSON b = object toJSON b = object
[ "_view_module" .= str "" [ "_view_module" .= str ""
......
...@@ -25,6 +25,7 @@ module IHaskell.IPython.Types ( ...@@ -25,6 +25,7 @@ module IHaskell.IPython.Types (
HistoryAccessType(..), HistoryAccessType(..),
HistoryReplyElement(..), HistoryReplyElement(..),
replyType, replyType,
showMessageType,
-- ** IPython display data message -- ** IPython display data message
DisplayData(..), DisplayData(..),
...@@ -63,7 +64,7 @@ data Profile = ...@@ -63,7 +64,7 @@ data Profile =
Profile Profile
{ ip :: IP -- ^ The IP on which to listen. { ip :: IP -- ^ The IP on which to listen.
, transport :: Transport -- ^ The transport mechanism. , transport :: Transport -- ^ The transport mechanism.
, stdinPort :: Port -- ^ The stdin channel port. , stdinPort :: Port -- ^ The stdin channel port.
, controlPort :: Port -- ^ The control channel port. , controlPort :: Port -- ^ The control channel port.
, hbPort :: Port -- ^ The heartbeat channel port. , hbPort :: Port -- ^ The heartbeat channel port.
, shellPort :: Port -- ^ The shell command port. , shellPort :: Port -- ^ The shell command port.
...@@ -114,7 +115,7 @@ instance ToJSON Transport where ...@@ -114,7 +115,7 @@ instance ToJSON Transport where
-------------------- IPython Kernelspec Types ---------------------- -------------------- IPython Kernelspec Types ----------------------
data KernelSpec = data KernelSpec =
KernelSpec KernelSpec
{ {
-- | Name shown to users to describe this kernel (e.g. "Haskell") -- | Name shown to users to describe this kernel (e.g. "Haskell")
kernelDisplayName :: String kernelDisplayName :: String
-- | Name for the kernel; unique kernel identifier (e.g. "haskell") -- | Name for the kernel; unique kernel identifier (e.g. "haskell")
......
...@@ -6,6 +6,7 @@ module IHaskell.Eval.Widgets ( ...@@ -6,6 +6,7 @@ module IHaskell.Eval.Widgets (
widgetSendClose, widgetSendClose,
widgetSendValue, widgetSendValue,
widgetPublishDisplay, widgetPublishDisplay,
widgetClearOutput,
relayWidgetMessages, relayWidgetMessages,
widgetHandler, widgetHandler,
) where ) where
...@@ -22,6 +23,7 @@ import System.IO.Unsafe (unsafePerformIO) ...@@ -22,6 +23,7 @@ import System.IO.Unsafe (unsafePerformIO)
import IHaskell.Display import IHaskell.Display
import IHaskell.Eval.Util (unfoldM) import IHaskell.Eval.Util (unfoldM)
import IHaskell.IPython.Types (showMessageType)
import IHaskell.IPython.Message.UUID import IHaskell.IPython.Message.UUID
import IHaskell.IPython.Message.Writer import IHaskell.IPython.Message.Writer
import IHaskell.Types import IHaskell.Types
...@@ -78,6 +80,10 @@ widgetSendValue widget = queue . JSONValue (Widget widget) ...@@ -78,6 +80,10 @@ widgetSendValue widget = queue . JSONValue (Widget widget)
widgetPublishDisplay :: (IHaskellWidget a, IHaskellDisplay b) => a -> b -> IO () widgetPublishDisplay :: (IHaskellWidget a, IHaskellDisplay b) => a -> b -> IO ()
widgetPublishDisplay widget disp = display disp >>= queue . DispMsg (Widget widget) widgetPublishDisplay widget disp = display disp >>= queue . DispMsg (Widget widget)
-- | Send a `clear_output` message as a [method .= custom] message
widgetClearOutput :: IHaskellWidget a => a -> Bool -> IO ()
widgetClearOutput widget wait = queue $ ClrOutput (Widget widget) wait
-- | Handle a single widget message. Takes necessary actions according to the message type, such as -- | Handle a single widget message. Takes necessary actions according to the message type, such as
-- opening comms, storing and updating widget representation in the kernel state etc. -- opening comms, storing and updating widget representation in the kernel state etc.
handleMessage :: (Message -> IO ()) handleMessage :: (Message -> IO ())
...@@ -142,16 +148,13 @@ handleMessage send replyHeader state msg = do ...@@ -142,16 +148,13 @@ handleMessage send replyHeader state msg = do
DispMsg widget disp -> do DispMsg widget disp -> do
dispHeader <- dupHeader replyHeader DisplayDataMessage dispHeader <- dupHeader replyHeader DisplayDataMessage
let dmsg = WidgetDisplay dispHeader "haskell" $ unwrap disp let dmsg = WidgetDisplay dispHeader "haskell" $ unwrap disp
uuid = getCommUUID widget sendMessage widget (toJSON $ CustomContent $ toJSON dmsg)
present = isJust $ Map.lookup uuid oldComms
-- If the widget is present, we send an update message on its comm. ClrOutput widget wait -> do
when present $ do header <- dupHeader replyHeader ClearOutputMessage
header <- dupHeader replyHeader CommDataMessage let cmsg = WidgetClear header wait
send $ CommData header uuid $ toJSON $ CustomContent $ toJSON dmsg sendMessage widget (toJSON $ CustomContent $ toJSON cmsg)
return state
where where
oldComms = openComms state oldComms = openComms state
...@@ -175,12 +178,27 @@ data WidgetDisplay = WidgetDisplay MessageHeader String [DisplayData] ...@@ -175,12 +178,27 @@ data WidgetDisplay = WidgetDisplay MessageHeader String [DisplayData]
instance ToJSON WidgetDisplay where instance ToJSON WidgetDisplay where
toJSON (WidgetDisplay replyHeader source ddata) = toJSON (WidgetDisplay replyHeader source ddata) =
let pbval = toJSON $ PublishDisplayData replyHeader source ddata let pbval = toJSON $ PublishDisplayData replyHeader source ddata
in object in toJSON $ IPythonMessage replyHeader pbval DisplayDataMessage
[ "header" .= replyHeader
, "parent_header" .= str "" -- Override toJSON for ClearOutput
, "metadata" .= str "{}" data WidgetClear = WidgetClear MessageHeader Bool
, "content" .= pbval
] instance ToJSON WidgetClear where
toJSON (WidgetClear replyHeader wait) =
let clrVal = toJSON $ ClearOutput replyHeader wait
in toJSON $ IPythonMessage replyHeader clrVal ClearOutputMessage
data IPythonMessage = IPythonMessage MessageHeader Value MessageType
instance ToJSON IPythonMessage where
toJSON (IPythonMessage replyHeader val msgType) =
object
[ "header" .= replyHeader
, "parent_header" .= str ""
, "metadata" .= str "{}"
, "content" .= val
, "msg_type" .= (toJSON . showMessageType $ msgType)
]
str :: String -> String str :: String -> String
str = id str = id
......
...@@ -208,7 +208,10 @@ data WidgetMsg = Open Widget Value Value ...@@ -208,7 +208,10 @@ data WidgetMsg = Open Widget Value Value
| |
-- ^ A json object that is sent to the widget without modifications. -- ^ A json object that is sent to the widget without modifications.
DispMsg Widget Display DispMsg Widget Display
-- ^ A 'display_data' message, sent as a [method .= custom] comm_msg -- ^ A 'display_data' message, sent as a [method .= custom] comm_msg
|
ClrOutput Widget Bool
-- ^ A 'clear_output' message, sent as a [method .= custom] comm_msg
deriving (Show, Typeable) deriving (Show, Typeable)
data WidgetMethod = UpdateState Value data WidgetMethod = UpdateState Value
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment