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
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
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
-> Message
-> MessageHeader
-> 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
KernelInfoReply
{ header = replyHeader
......@@ -172,6 +177,7 @@ replyTo config _ _ KernelInfoRequest{} replyHeader =
, implementationVersion = kernelImplVersion config
, banner = kernelBanner config
, protocolVersion = kernelProtocolVersion config
, status = Ok
}
replyTo config _ _ CommInfoRequest{} replyHeader =
......@@ -198,12 +204,9 @@ replyTo config execCount interface req@ExecuteRequest { getCode = code } replyHe
sendOutput x =
send $ PublishDisplayData
outputHeader
(languageName $ kernelLanguageInfo
config)
(displayOutput config x)
in run config code clearOutput sendOutput
liftIO . send $ PublishDisplayData outputHeader (languageName $ kernelLanguageInfo config)
(displayResult config res)
liftIO . send $ PublishDisplayData outputHeader (displayResult config res)
idleHeader <- dupHeader replyHeader StatusMessage
......
......@@ -178,8 +178,7 @@ displayDataParser :: LByteString -> Message
displayDataParser = requestParser $ \obj -> do
dataDict :: Object <- obj .: "data"
let displayDatas = makeDisplayDatas dataDict
maybeSource <- obj .:? "source"
return $ PublishDisplayData noHeader (fromMaybe "" maybeSource) displayDatas
return $ PublishDisplayData noHeader displayDatas
requestParser parser content =
case parseEither parser decoded of
......
......@@ -20,6 +20,7 @@ instance ToJSON LanguageInfo where
, "version" .= languageVersion info
, "file_extension" .= languageFileExtension info
, "codemirror_mode" .= languageCodeMirrorMode info
, "pygments_lexer" .= languagePygmentsLexer info
]
-- Convert message bodies into JSON.
......@@ -31,6 +32,7 @@ instance ToJSON Message where
, "implementation" .= implementation rep
, "implementation_version" .= implementationVersion rep
, "language_info" .= languageInfo rep
, "status" .= show (status rep)
]
toJSON CommInfoReply
......@@ -38,7 +40,9 @@ instance ToJSON Message where
, commInfo = commInfo
} =
object
[ "comms" .= Map.map (\comm -> object ["target_name" .= comm]) commInfo ]
[ "comms" .= Map.map (\comm -> object ["target_name" .= comm]) commInfo
, "status" .= string "ok"
]
toJSON ExecuteRequest
{ getCode = code
......@@ -76,9 +80,9 @@ instance ToJSON Message where
object ["execution_state" .= executionState]
toJSON PublishStream { streamType = streamType, streamContent = content } =
object ["data" .= content, "name" .= streamType]
toJSON PublishDisplayData { source = src, displayData = datas } =
toJSON PublishDisplayData { displayData = datas } =
object
["source" .= src, "metadata" .= object [], "data" .= object (map displayDataToJson datas)]
["metadata" .= object [], "data" .= object (map displayDataToJson datas)]
toJSON PublishOutput { executionCount = execCount, reprText = reprText } =
object
......@@ -109,7 +113,9 @@ instance ToJSON Message where
]
toJSON ShutdownReply { restartPending = restart } =
object ["restart" .= restart]
object ["restart" .= restart
, "status" .= string "ok"
]
toJSON ClearOutput { wait = wait } =
object ["wait" .= wait]
......@@ -132,7 +138,9 @@ instance ToJSON Message where
object ["comm_id" .= commUuid req, "data" .= commData req]
toJSON req@HistoryReply{} =
object ["history" .= map tuplify (historyReply req)]
object ["history" .= map tuplify (historyReply req)
, "status" .= string "ok"
]
where
tuplify (HistoryReplyElement sess linum res) = (sess, linum, case res of
Left inp -> toJSON inp
......
......@@ -276,6 +276,7 @@ data LanguageInfo =
, languageVersion :: String -- ^ GHC 7.6.3
, languageFileExtension :: String -- ^ .hs
, languageCodeMirrorMode :: String -- ^ 'ihaskell'. can be 'null'
, languagePygmentsLexer :: String
}
deriving (Show, Eq)
......@@ -299,6 +300,7 @@ data Message =
, implementation :: String -- ^ e.g. IHaskell
, implementationVersion :: String -- ^ The version of the implementation
, languageInfo :: LanguageInfo
, status :: ExecuteReplyStatus
}
|
-- | A request from a frontend for information about the comms.
......@@ -366,7 +368,6 @@ data Message =
|
PublishDisplayData
{ header :: MessageHeader
, source :: String -- ^ The name of the data source.
, displayData :: [DisplayData] -- ^ A list of data representations.
}
|
......
......@@ -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
-- kernel info reply is a static object (all info is hard coded into the representation of that
-- 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
(state, KernelInfoReply
{ header = replyHeader
......@@ -268,7 +274,9 @@ replyTo _ KernelInfoRequest{} replyHeader state =
, languageVersion = VERSION_ghc
, languageFileExtension = ".hs"
, languageCodeMirrorMode = "ihaskell"
, languagePygmentsLexer = "Haskell"
}
, status = Ok
})
replyTo _ CommInfoRequest{} replyHeader state =
......
......@@ -145,7 +145,7 @@ handleMessage send replyHeader state msg = do
DispMsg widget disp -> do
dispHeader <- dupHeader replyHeader DisplayDataMessage
let dmsg = WidgetDisplay dispHeader "haskell" $ unwrap disp
let dmsg = WidgetDisplay dispHeader $ unwrap disp
sendMessage widget (toJSON $ CustomContent $ toJSON dmsg)
ClrOutput widget wait -> do
......@@ -170,11 +170,11 @@ handleMessage send replyHeader state msg = do
unwrap (Display ddatas) = ddatas
-- 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
toJSON (WidgetDisplay replyHeader source ddata) =
let pbval = toJSON $ PublishDisplayData replyHeader source ddata
toJSON (WidgetDisplay replyHeader ddata) =
let pbval = toJSON $ PublishDisplayData replyHeader ddata
in toJSON $ IPythonMessage replyHeader pbval DisplayDataMessage
-- Override toJSON for ClearOutput
......
......@@ -63,7 +63,7 @@ publishResult send replyHeader displayed updateNeeded pagerOutput usePager resul
sendOutput (ManyDisplay manyOuts) = mapM_ sendOutput manyOuts
sendOutput (Display outs) = do
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 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