Add Jupyter Xeus support

pull/6/head
Ibrahim Numanagić 2021-11-01 03:31:24 -07:00
parent c46b513ea7
commit 5503e71ace
6 changed files with 184 additions and 0 deletions

3
.gitignore vendored
View File

@ -51,3 +51,6 @@ Thumbs.db
.idea
.mypy_cache
.vscode
extra/jupyter/share/jupyter/kernels/codon/kernel.json
scratch.seq

View File

@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.14)
project(CodonJupyter)
include(GNUInstallDirs)
# We generate the kernel.json file, given the installation prefix and the executable name
configure_file (
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/codon/kernel.json.in"
"${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels/codon/kernel.json")
set(CPM_DOWNLOAD_VERSION 0.32.3)
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
message(STATUS "Downloading CPM.cmake...")
file(DOWNLOAD https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake ${CPM_DOWNLOAD_LOCATION})
endif()
include(${CPM_DOWNLOAD_LOCATION})
CPMAddPackage(
NAME libzmq
VERSION 4.3.4
URL https://github.com/zeromq/libzmq/releases/download/v4.3.4/zeromq-4.3.4.tar.gz
OPTIONS "WITH_PERF_TOOL OFF"
"ZMQ_BUILD_TESTS OFF"
"ENABLE_CPACK OFF"
"BUILD_SHARED OFF"
"WITH_LIBSODIUM OFF")
CPMAddPackage(
NAME cppzmq
URL https://github.com/zeromq/cppzmq/archive/refs/tags/v4.8.1.tar.gz
VERSION 4.8.1
OPTION "CPPZMQ_BUILD_TESTS OFF")
if(cppzmq_ADDED)
set_target_properties(unit_tests PROPERTIES EXCLUDE_FROM_ALL ON)
endif()
CPMAddPackage(
NAME xtl
GITHUB_REPOSITORY "xtensor-stack/xtl"
VERSION 0.7.3
GIT_TAG 0.7.3
OPTIONS "BUILD_TESTS OFF")
CPMAddPackage(
NAME json
GITHUB_REPOSITORY "nlohmann/json"
VERSION 3.10.4)
CPMAddPackage(
NAME xeus
GITHUB_REPOSITORY "jupyter-xeus/xeus"
VERSION 2.2.0
GIT_TAG 2.2.0
OPTIONS "BUILD_EXAMPLES OFF"
"XEUS_BUILD_SHARED_LIBS OFF"
"XEUS_STATIC_DEPENDENCIES ON")
if (xeus_ADDED)
install(TARGETS nlohmann_json EXPORT xeus-targets)
endif()
set(CMAKE_CXX_STANDARD 14)
find_package(Threads)
set(CODON_SRC
src/codon.cpp
src/codon.h)
add_executable(codon-jupyter src/main.cpp ${CODON_SRC})
add_dependencies(codon-jupyter xeus-static)
target_link_libraries(codon-jupyter PRIVATE xeus-static Threads::Threads) # codonc)
set_target_properties(codon-jupyter PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
# Install kernel
install(TARGETS codon-jupyter RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
set(XJUPYTER_DATA_DIR "share/jupyter" CACHE STRING "Jupyter data directory")
set(KERNELSPEC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/share/jupyter/kernels)
install(DIRECTORY ${KERNELSPEC_DIR} DESTINATION ${XJUPYTER_DATA_DIR} PATTERN "*.in" EXCLUDE)

View File

@ -0,0 +1,9 @@
{
"display_name": "Codon",
"argv": [
"@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@/@EXECUTABLE_NAME@",
"-f",
"{connection_file}"
],
"language": "python"
}

View File

@ -0,0 +1,46 @@
#include "codon.h"
#include <iostream>
#include <nlohmann/json.hpp>
#include <xeus/xhelper.hpp>
using std::move;
using std::string;
namespace nl = nlohmann;
namespace codon {
nl::json CodonJupyter::execute_request_impl(int execution_counter, const string &code,
bool silent, bool store_history,
nl::json user_expressions,
bool allow_stdin) {
nl::json pub_data;
pub_data["text/plain"] = "Hello World !!";
publish_execution_result(execution_counter, move(pub_data), nl::json::object());
// publish_execution_error("TypeError", "123", {"!@#$", "*(*"});
return xeus::create_successful_reply();
}
void CodonJupyter::configure_impl() {}
nl::json CodonJupyter::complete_request_impl(const string &code, int cursor_pos) {
return xeus::create_complete_reply({}, cursor_pos, cursor_pos);
}
nl::json CodonJupyter::inspect_request_impl(const string &code, int cursor_pos,
int detail_level) {
return xeus::create_inspect_reply();
}
nl::json CodonJupyter::is_complete_request_impl(const string &code) {
return xeus::create_is_complete_reply("complete");
}
nl::json CodonJupyter::kernel_info_request_impl() {
return xeus::create_info_reply("", "codon_kernel", "0.1.0", "python", "3.7",
"text/x-python", ".seq");
}
void CodonJupyter::shutdown_request_impl() {}
} // namespace codon

View File

@ -0,0 +1,29 @@
#pragma once
#include <nlohmann/json.hpp>
#include <xeus/xinterpreter.hpp>
using xeus::xinterpreter;
namespace nl = nlohmann;
namespace codon {
class CodonJupyter : public xinterpreter {
private:
void configure_impl() override;
nl::json execute_request_impl(int execution_counter, const std::string &code,
bool silent, bool store_history,
nl::json user_expressions, bool allow_stdin) override;
nl::json complete_request_impl(const std::string &code, int cursor_pos) override;
nl::json inspect_request_impl(const std::string &code, int cursor_pos,
int detail_level) override;
nl::json is_complete_request_impl(const std::string &code) override;
nl::json kernel_info_request_impl() override;
void shutdown_request_impl() override;
};
} // namespace codon

View File

@ -0,0 +1,24 @@
#include "codon.h"
#include <memory>
#include <string>
#include <xeus/xkernel.hpp>
#include <xeus/xkernel_configuration.hpp>
#include <xeus/xserver_zmq.hpp>
using std::make_unique;
using std::move;
using std::string;
int main(int argc, char *argv[]) {
string file_name = (argc == 1) ? "connection.json" : argv[2];
xeus::xconfiguration config = xeus::load_configuration(file_name);
auto context = xeus::make_context<zmq::context_t>();
auto interpreter = make_unique<codon::CodonJupyter>();
xeus::xkernel kernel(config, xeus::get_user_name(), move(context), move(interpreter),
xeus::make_xserver_zmq);
kernel.start();
return 0;
}