Unverified Commit 2e4b4d3e authored by Vaibhav Sagar's avatar Vaibhav Sagar Committed by GitHub

Merge pull request #886 from MMesch/jupyterlab_compatibility

Jupyterlab compatibility
parents fbd91afb ff6f1fa7
...@@ -41,6 +41,13 @@ stack install --fast ...@@ -41,6 +41,13 @@ stack install --fast
ihaskell install --stack ihaskell install --stack
``` ```
if you want to use jupyterlab, you need to install the jupyterlab ihaskell
extension to get syntax highlighting with:
```bash
jupyter labextension install ihaskell_labextension
```
## Mac ## Mac
You need to have [Homebrew](https://brew.sh) installed. You need to have [Homebrew](https://brew.sh) installed.
......
*.bundle.*
lib/
node_modules/
*.egg-info/
.ipynb_checkpoints
# ihaskell_labextension
Haskell Syntax Highlighting in Jupyterlab
## Prerequisites
* JupyterLab
## Installation
```bash
jupyter labextension install ihaskell_labextension
```
## Development
For a development install (requires npm version 4 or later), do the following in the repository directory:
```bash
npm install
npm run build
jupyter labextension link .
```
To rebuild the package and the JupyterLab app:
```bash
npm run build
jupyter lab build
```
This diff is collapsed.
{
"name": "ihaskell_jupyterlab",
"version": "0.0.1",
"description": "JupyterLab extension",
"keywords": [
"jupyter",
"jupyterlab",
"jupyterlab-extension"
],
"homepage": "https://github.com/xxx",
"bugs": {
"url": "https://github.com/xxx/issues"
},
"license": "BSD-3-Clause",
"author": "MMesch",
"files": [
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/xxx/jupyterlab-sos.git"
},
"scripts": {
"build": "tsc",
"clean": "rimraf lib",
"watch": "tsc -w"
},
"dependencies": {
"@jupyterlab/application": "^0.16.1",
"@jupyterlab/apputils": "^0.16.2",
"@jupyterlab/docregistry": "^0.16.1",
"@jupyterlab/notebook": "^0.16.1",
"@jupyterlab/services": "^2.0.1",
"@phosphor/disposable": "^1.1.2"
},
"devDependencies": {
"rimraf": "^2.6.1",
"typescript": "~2.6.0"
},
"jupyterlab": {
"extension": true
}
}
import * as CodeMirror from 'codemirror';
import 'codemirror/lib/codemirror';
import 'codemirror/mode/haskell/haskell';
CodeMirror.defineMode("ihaskell", (config) => {
let hmode = CodeMirror.getMode(config, "haskell");
return CodeMirror.multiplexingMode(
hmode,
{
open: /:(?=!)/, // Matches : followed by !, but doesn't consume !
close: /^(?!!)/, // Matches start of line not followed by !, doesn't consume character
mode: CodeMirror.getMode(config, "text/plain"),
delimStyle: "delimit"
}
);
});
CodeMirror.defineMIME("text/x-ihaskell", "ihaskell");
CodeMirror.modeInfo.push({
ext: ['hs'],
mime: "text/x-ihaskell",
mode: 'ihaskell',
name: 'ihaskell'
});
import {
JupyterLab, JupyterLabPlugin
} from '@jupyterlab/application';
import './codemirror-ihaskell';
import '../style/index.css';
/**
* Initialization data for the extension1 extension.
*/
const extension: JupyterLabPlugin<void> = {
id: 'ihaskell',
autoStart: true,
requires: [],
activate: (app: JupyterLab) =>
{
app.serviceManager.ready
.then(() => {defineIHaskell()});
}
};
function defineIHaskell() {
console.log('ihaskell codemirror activated');
}
export default extension;
{
"compilerOptions": {
"declaration": true,
"noImplicitAny": false,
"noEmitOnError": true,
"noUnusedLocals": true,
"allowSyntheticDefaultImports": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es2015",
"outDir": "./lib",
"lib": ["dom", "es2015"],
"types": []
},
"include": ["src/*"]
}
This diff is collapsed.
...@@ -163,7 +163,12 @@ replyTo :: MonadIO m ...@@ -163,7 +163,12 @@ replyTo :: MonadIO m
-> Message -> Message
-> MessageHeader -> MessageHeader
-> m Message -> m Message
replyTo config _ _ KernelInfoRequest{} replyHeader = replyTo config _ interface KernelInfoRequest{} replyHeader = do
let send = writeChan (iopubChannel interface)
idleHeader <- dupHeader replyHeader StatusMessage
liftIO . send $ PublishStatus idleHeader Idle
return return
KernelInfoReply KernelInfoReply
{ header = replyHeader { header = replyHeader
...@@ -172,6 +177,7 @@ replyTo config _ _ KernelInfoRequest{} replyHeader = ...@@ -172,6 +177,7 @@ replyTo config _ _ KernelInfoRequest{} replyHeader =
, implementationVersion = kernelImplVersion config , implementationVersion = kernelImplVersion config
, banner = kernelBanner config , banner = kernelBanner config
, protocolVersion = kernelProtocolVersion config , protocolVersion = kernelProtocolVersion config
, status = Ok
} }
replyTo config _ _ CommInfoRequest{} replyHeader = replyTo config _ _ CommInfoRequest{} replyHeader =
...@@ -198,12 +204,9 @@ replyTo config execCount interface req@ExecuteRequest { getCode = code } replyHe ...@@ -198,12 +204,9 @@ replyTo config execCount interface req@ExecuteRequest { getCode = code } replyHe
sendOutput x = sendOutput x =
send $ PublishDisplayData send $ PublishDisplayData
outputHeader outputHeader
(languageName $ kernelLanguageInfo
config)
(displayOutput config x) (displayOutput config x)
in run config code clearOutput sendOutput in run config code clearOutput sendOutput
liftIO . send $ PublishDisplayData outputHeader (languageName $ kernelLanguageInfo config) liftIO . send $ PublishDisplayData outputHeader (displayResult config res)
(displayResult config res)
idleHeader <- dupHeader replyHeader StatusMessage idleHeader <- dupHeader replyHeader StatusMessage
......
...@@ -178,8 +178,7 @@ displayDataParser :: LByteString -> Message ...@@ -178,8 +178,7 @@ displayDataParser :: LByteString -> Message
displayDataParser = requestParser $ \obj -> do displayDataParser = requestParser $ \obj -> do
dataDict :: Object <- obj .: "data" dataDict :: Object <- obj .: "data"
let displayDatas = makeDisplayDatas dataDict let displayDatas = makeDisplayDatas dataDict
maybeSource <- obj .:? "source" return $ PublishDisplayData noHeader displayDatas
return $ PublishDisplayData noHeader (fromMaybe "" maybeSource) displayDatas
requestParser parser content = requestParser parser content =
case parseEither parser decoded of case parseEither parser decoded of
......
...@@ -20,6 +20,7 @@ instance ToJSON LanguageInfo where ...@@ -20,6 +20,7 @@ instance ToJSON LanguageInfo where
, "version" .= languageVersion info , "version" .= languageVersion info
, "file_extension" .= languageFileExtension info , "file_extension" .= languageFileExtension info
, "codemirror_mode" .= languageCodeMirrorMode info , "codemirror_mode" .= languageCodeMirrorMode info
, "pygments_lexer" .= languagePygmentsLexer info
] ]
-- Convert message bodies into JSON. -- Convert message bodies into JSON.
...@@ -31,6 +32,7 @@ instance ToJSON Message where ...@@ -31,6 +32,7 @@ instance ToJSON Message where
, "implementation" .= implementation rep , "implementation" .= implementation rep
, "implementation_version" .= implementationVersion rep , "implementation_version" .= implementationVersion rep
, "language_info" .= languageInfo rep , "language_info" .= languageInfo rep
, "status" .= show (status rep)
] ]
toJSON CommInfoReply toJSON CommInfoReply
...@@ -38,7 +40,9 @@ instance ToJSON Message where ...@@ -38,7 +40,9 @@ instance ToJSON Message where
, commInfo = commInfo , commInfo = commInfo
} = } =
object object
[ "comms" .= Map.map (\comm -> object ["target_name" .= comm]) commInfo ] [ "comms" .= Map.map (\comm -> object ["target_name" .= comm]) commInfo
, "status" .= string "ok"
]
toJSON ExecuteRequest toJSON ExecuteRequest
{ getCode = code { getCode = code
...@@ -76,9 +80,9 @@ instance ToJSON Message where ...@@ -76,9 +80,9 @@ instance ToJSON Message where
object ["execution_state" .= executionState] object ["execution_state" .= executionState]
toJSON PublishStream { streamType = streamType, streamContent = content } = toJSON PublishStream { streamType = streamType, streamContent = content } =
object ["data" .= content, "name" .= streamType] object ["data" .= content, "name" .= streamType]
toJSON PublishDisplayData { source = src, displayData = datas } = toJSON PublishDisplayData { displayData = datas } =
object object
["source" .= src, "metadata" .= object [], "data" .= object (map displayDataToJson datas)] ["metadata" .= object [], "data" .= object (map displayDataToJson datas)]
toJSON PublishOutput { executionCount = execCount, reprText = reprText } = toJSON PublishOutput { executionCount = execCount, reprText = reprText } =
object object
...@@ -109,7 +113,9 @@ instance ToJSON Message where ...@@ -109,7 +113,9 @@ instance ToJSON Message where
] ]
toJSON ShutdownReply { restartPending = restart } = toJSON ShutdownReply { restartPending = restart } =
object ["restart" .= restart] object ["restart" .= restart
, "status" .= string "ok"
]
toJSON ClearOutput { wait = wait } = toJSON ClearOutput { wait = wait } =
object ["wait" .= wait] object ["wait" .= wait]
...@@ -132,7 +138,9 @@ instance ToJSON Message where ...@@ -132,7 +138,9 @@ instance ToJSON Message where
object ["comm_id" .= commUuid req, "data" .= commData req] object ["comm_id" .= commUuid req, "data" .= commData req]
toJSON req@HistoryReply{} = toJSON req@HistoryReply{} =
object ["history" .= map tuplify (historyReply req)] object ["history" .= map tuplify (historyReply req)
, "status" .= string "ok"
]
where where
tuplify (HistoryReplyElement sess linum res) = (sess, linum, case res of tuplify (HistoryReplyElement sess linum res) = (sess, linum, case res of
Left inp -> toJSON inp Left inp -> toJSON inp
......
...@@ -276,6 +276,7 @@ data LanguageInfo = ...@@ -276,6 +276,7 @@ data LanguageInfo =
, languageVersion :: String -- ^ GHC 7.6.3 , languageVersion :: String -- ^ GHC 7.6.3
, languageFileExtension :: String -- ^ .hs , languageFileExtension :: String -- ^ .hs
, languageCodeMirrorMode :: String -- ^ 'ihaskell'. can be 'null' , languageCodeMirrorMode :: String -- ^ 'ihaskell'. can be 'null'
, languagePygmentsLexer :: String
} }
deriving (Show, Eq) deriving (Show, Eq)
...@@ -299,6 +300,7 @@ data Message = ...@@ -299,6 +300,7 @@ data Message =
, implementation :: String -- ^ e.g. IHaskell , implementation :: String -- ^ e.g. IHaskell
, implementationVersion :: String -- ^ The version of the implementation , implementationVersion :: String -- ^ The version of the implementation
, languageInfo :: LanguageInfo , languageInfo :: LanguageInfo
, status :: ExecuteReplyStatus
} }
| |
-- | A request from a frontend for information about the comms. -- | A request from a frontend for information about the comms.
...@@ -366,7 +368,6 @@ data Message = ...@@ -366,7 +368,6 @@ data Message =
| |
PublishDisplayData PublishDisplayData
{ header :: MessageHeader { header :: MessageHeader
, source :: String -- ^ The name of the data source.
, displayData :: [DisplayData] -- ^ A list of data representations. , displayData :: [DisplayData] -- ^ A list of data representations.
} }
| |
......
...@@ -255,7 +255,13 @@ replyTo :: ZeroMQInterface -> Message -> MessageHeader -> KernelState -> Interpr ...@@ -255,7 +255,13 @@ replyTo :: ZeroMQInterface -> Message -> MessageHeader -> KernelState -> Interpr
-- Reply to kernel info requests with a kernel info reply. No computation needs to be done, as a -- Reply to kernel info requests with a kernel info reply. No computation needs to be done, as a
-- kernel info reply is a static object (all info is hard coded into the representation of that -- kernel info reply is a static object (all info is hard coded into the representation of that
-- message type). -- message type).
replyTo _ KernelInfoRequest{} replyHeader state = replyTo interface KernelInfoRequest{} replyHeader state = do
let send msg = liftIO $ writeChan (iopubChannel interface) msg
-- Notify the frontend that the Kernel is idle
idleHeader <- liftIO $ dupHeader replyHeader StatusMessage
send $ PublishStatus idleHeader Idle
return return
(state, KernelInfoReply (state, KernelInfoReply
{ header = replyHeader { header = replyHeader
...@@ -268,7 +274,9 @@ replyTo _ KernelInfoRequest{} replyHeader state = ...@@ -268,7 +274,9 @@ replyTo _ KernelInfoRequest{} replyHeader state =
, languageVersion = VERSION_ghc , languageVersion = VERSION_ghc
, languageFileExtension = ".hs" , languageFileExtension = ".hs"
, languageCodeMirrorMode = "ihaskell" , languageCodeMirrorMode = "ihaskell"
, languagePygmentsLexer = "Haskell"
} }
, status = Ok
}) })
replyTo _ CommInfoRequest{} replyHeader state = replyTo _ CommInfoRequest{} replyHeader state =
......
...@@ -145,7 +145,7 @@ handleMessage send replyHeader state msg = do ...@@ -145,7 +145,7 @@ 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 $ unwrap disp
sendMessage widget (toJSON $ CustomContent $ toJSON dmsg) sendMessage widget (toJSON $ CustomContent $ toJSON dmsg)
ClrOutput widget wait -> do ClrOutput widget wait -> do
...@@ -170,11 +170,11 @@ handleMessage send replyHeader state msg = do ...@@ -170,11 +170,11 @@ handleMessage send replyHeader state msg = do
unwrap (Display ddatas) = ddatas unwrap (Display ddatas) = ddatas
-- Override toJSON for PublishDisplayData for sending Display messages through [method .= custom] -- Override toJSON for PublishDisplayData for sending Display messages through [method .= custom]
data WidgetDisplay = WidgetDisplay MessageHeader String [DisplayData] data WidgetDisplay = WidgetDisplay MessageHeader [DisplayData]
instance ToJSON WidgetDisplay where instance ToJSON WidgetDisplay where
toJSON (WidgetDisplay replyHeader source ddata) = toJSON (WidgetDisplay replyHeader ddata) =
let pbval = toJSON $ PublishDisplayData replyHeader source ddata let pbval = toJSON $ PublishDisplayData replyHeader ddata
in toJSON $ IPythonMessage replyHeader pbval DisplayDataMessage in toJSON $ IPythonMessage replyHeader pbval DisplayDataMessage
-- Override toJSON for ClearOutput -- Override toJSON for ClearOutput
......
...@@ -63,7 +63,7 @@ publishResult send replyHeader displayed updateNeeded pagerOutput usePager resul ...@@ -63,7 +63,7 @@ publishResult send replyHeader displayed updateNeeded pagerOutput usePager resul
sendOutput (ManyDisplay manyOuts) = mapM_ sendOutput manyOuts sendOutput (ManyDisplay manyOuts) = mapM_ sendOutput manyOuts
sendOutput (Display outs) = do sendOutput (Display outs) = do
header <- dupHeader replyHeader DisplayDataMessage header <- dupHeader replyHeader DisplayDataMessage
send $ PublishDisplayData header "haskell" $ map (convertSvgToHtml . prependCss) outs send $ PublishDisplayData header $ map (convertSvgToHtml . prependCss) outs
convertSvgToHtml (DisplayData MimeSvg svg) = html $ makeSvgImg $ base64 $ E.encodeUtf8 svg convertSvgToHtml (DisplayData MimeSvg svg) = html $ makeSvgImg $ base64 $ E.encodeUtf8 svg
convertSvgToHtml x = x convertSvgToHtml x = x
......
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