Home

Awesome

Hercutalk - A Jupyter Kernel for Logtalk

A Jupyter kernel for Logtalk based on prolog-jupyter-kernel and IPython kernel.

This project is a fork of the prolog-jupyter-kernel project (developed by Anne Brecklinghaus in her Master's thesis at the University of Düsseldorf under the supervision of Michael Leuschel and Philipp Körner) and still under development. It includes back-ports of recent patches and improvements by Michael Leuschel, dgelessus, and Silas Kraume. Major changes are committed and more are expected. Furthermore, no liability is accepted for correctness and completeness (see the LICENSE file).

🙏 Sponsored by Permion and GitHub Sponsors.

Supported Logtalk version

Logtalk 3.81.0 (or later version) plus at least one of the supported Prolog backends. The LOGTALKHOME and LOGTALKUSER environment variables must be defined.

Supported Prolog backends and versions

Note that a public online use of this kernel (instead of private or local) may be restricted to a subset of these backends (notably, due to some systems requiring commercial licenses).

The kernel is implemented in a way that basically all functionality except the loading of configuration files can easily be overridden. This is especially useful for extending the kernel for further Prolog backends or running code with a different version of a backend. For further information about this, see Configuration.

Also see the JupyterLab Logtalk CodeMirror Extension for syntax highlighting of Logtalk code in JupyterLab (forked from the JupyterLab Prolog CodeMirror Extension).

Examples

The directory notebooks contains some example Juypter notebooks, including a Logtalk short tutorial and a notebook giving an overview of the kernel's features and its implementation. Note that all of them can be viewed with nbviewer without having to install the kernel.

Install

The kernel is provided as a Python package on the Python Package Index and can be installed with pip:

python3 -m pip install --upgrade logtalk-jupyter-kernel
python3 -m logtalk_kernel.install

There are the following options which can be seen when running python3 -m logtalk_kernel.install --help

Uninstall

python3 -m pip uninstall logtalk_kernel
jupyter kernelspec remove logtalk_kernel

Running

Logtalk notebooks can be run using JupyterLab, Jupyter notebook, or VSCode.

Running using JupyterLab

Simply start JupyterLab (e.g. by typing jupyter-lab in a shell) and then click on the Logtalk Notebook (or Logtalk Console) icon in the Launcher or open an existing notebook.

Running using Jupyter notebook

Simply start Jupyter notebook (e.g. by typing jupyter notebook in a shell) and then open an existing notebook.

Running using VSCode

Simply open an existing notebook. Ensure that the Logtalk plug-in for VSCode for syntax highlighting in code cells.

Configuration

The kernel can be configured by defining a Python config file named logtalk_kernel_config.py. The kernel will look for this file in the Jupyter config path (can be retrieved with jupyter --paths) and the current working directory. An example of such a configuration file with an explanation of the options and their default values commented out can be found here.

Note: If a config file exists in the current working directory, it overrides values from other configuration files.

In general, the kernel can be configured to use a different Prolog backend (which is responsible for code execution) or kernel implementation. Furthermore, it can be configured to use another Prolog backend altogether which might not be supported by default. The following options can be configured:

If the given program_arguments are invalid, the kernel waits for a response from the server which it will never receive. In that state it is not able to log any exception and instead, nothing happens. To facilitate finding the cause of the error, before trying to start the Logtalk server, the arguments and the directory from which they are tried to be executed are logged.

Defining environment variables for notebooks

Notebooks may require defining environment variables. For example, a notebook running one of the Java integration examples found in the Logtalk distribution may require the CLASSPATH environment variable to be set. This can be easily accomplished by adding a logtalk_kernel_config.py file to the notebook directory and using the os.environ Python dictionary. For the Logtalk document_converter example, which uses Apache Tika, assuming we copied the JAR file to the notebook directory, we could write:

os.environ['CLASSPATH'] = './tika-app-2.8.0.jar'

Using virtual environment for Logtalk packs

Notebooks may require loading Logtalk packs. Ideally, when sharing notebooks with other users, those packs should be installed in a virtual environment to avoid any conflicts with user installed packs or pack versions. The lgtenv script provided by the Logtalk distribution can be used to create the packs virtual environment in the same directory as the notebook. For example:

$ cd my_notebook_directory
$ lgtenv -p logtalk_packs

The packs can be pre-installed before sharing e.g. an archive with the notebook directory contents. Alternatively, installing the packs can be left to the user by providing a requirements.lgt file. For example:

registry(talkshow, 'https://github.com/LogtalkDotOrg/talkshow').
pack(talkshow, lflat, 2:1:0).

In this case, the user will need to run (possibly from a notebook code cell) the query:

?- logtalk_load(packs(loader)), packs::restore('requirements.lgt').

We also must ensure that the virtual environment will be used when the notebook runs. The best solution is to create a settings.lgt file in the same directory as the notebook defining the logtalk_packs library alias. For example, assuming a logtalk_packs sub-directory for the virtual environment:

:- multifile(logtalk_library_path/2).
:- dynamic(logtalk_library_path/2).

:- initialization((
	logtalk_load_context(directory, Directory),
	atom_concat(Directory, logtalk_packs, VirtualEnvironment),
	asserta(logtalk_library_path(logtalk_packs, VirtualEnvironment))
)).

Changing the Prolog backend in the fly

In most cases, the following shortcuts can be used:

If the shortcuts don't work due to some unusal Logtalk or Prolog backend setup, the jupyter::set_prolog_backend(+Backend) predicate is provided. In order for this to work, the configured backend_data dictionary needs to contain data for more than one Prolog backend. For example (in a notebook code cell):

jupyter::set_prolog_backend('xvmlgt.sh').

The predicate argument is the name of the integration script used to run Logtalk. On Windows, always use the PowerShell scripts (e.g. sicstuslgt.ps1). On POSIX systems, use the ones that work for your Logtalk installation (e.g. if you're using Logtalk with Trealla Prolog with a setup that requires the .sh extension when running the integration script, then use tplgt.sh instead of just tplgt).

Development

Requirements

The installation was tested with macOS 14.5, Ubuntu 20.0.4, and Windows 10.

Install

python3 -m pip install --upgrade jupyterlab
git clone https://github.com/LogtalkDotOrg/logtalk-jupyter-kernel
cd logtalk-jupyter-kernel
make install

By default, make install uses sys.prefix. If it fails with a permission error, you can retry using either sudo make install or repeat its last step using python3 -m logtalk_kernel.install --user or python3 -m logtalk_kernel.install --prefix PREFIX.

On Ubuntu, if make install fails with an error, try to update pip to its latest version by running python3 -m pip install --upgrade pip.

Uninstall

cd logtalk-jupyter-kernel
make clean

Local Changes

In general, in order for local code adjustments to take effect, the kernel needs to be reinstalled. When installing the local project in editable mode with python3 -m pip install -e . (e.g. by running make), restarting the kernel suffices.

Adjustments of the Logtalk server code are loaded when the server is restarted. Thus, when changing Logtalk code only, instead of restarting the whole kernel, it can be interrupted, which causes the Logtalk server to be restarted.

Building and publishing

python3 -m build .
twine upload dist/logtalk-jupyter-kernel-VERSION.tar.gz dist/logtalk_jupyter_kernel-VERSION-py3-none-any.whl

Debugging

If you get a Failed to start the Kernel. error after selecting the Logtalk kernel, make sure that the LOGTALKHOME and LOGTALKUSER environment variables are defined.

Usually, if the execution of a goal causes an exception, the corresponding Logtalk error message is captured and displayed in the Jupyter frontend. However, in case something goes wrong unexpectedly or the query does not terminate, the Logtalk server might not be able to send a response to the client. In that case, the user can only see that the execution does not terminate without any information about the error or output that might have been produced. However, it is possible to write logging messages and access any potential output, which might facilitate finding the cause of the error.

Debugging the server code is not possible in the usual way by tracing invocations. Furthermore, all messages exchanged with the client are written to the standard streams. Therefore, printing helpful debugging messages does not work either. Instead, if server_logging is configured, messages can be written to a log file by calling log/1 or log/2 from the jupyter_logging object. By default, only the responses sent to the client are logged.

When a query is executed, all its output is written to a file named .server_output, which is deleted afterwards by jupyter_query_handling::delete_output_file. If an error occurs during the actual execution, the file cannot be deleted and thus, the output of the goal can be accessed. Otherwise, the deletion might be prevented.

Furthermore, the server might send a response which the client cannot handle. In that case, logging for the Python code can be enabled by configuring jupyter_logging. For instance, the client logs the responses received from the server.

When the Logtalk code makes calls to foreign language libraries (notably C or C++ code), it's possible that output is generated that is not diverted to a file when the kernel redirects the Prolog output streams. This unexpected output is most likely not a valid JSON payload and thus breaks communication between the notebook and the kernel. In this case, the notebook displays the following error:

Something went wrong
The Logtalk server needs to be restarted

These issues can be debugged by running the problematic query in a terminal after diverting the Prolog output streams to a file. For example, assuming in the Prolog backend you're using the stream redirecting uses a set_stream/2 predicate:

?- open(out, write, S),
   set_stream(S, alias(current_output)),
   set_stream(S, alias(user_output)),
   set_stream(S, alias(user_error)),
   goal,
   close(S).

If you get any output while the goal is running (e.g. foreign library debugging messages), you will need to find a way to turn off that output.

Prolog backend requirements

Adding support for other Prolog backends requires:

Overriding the Kernel Implementation

The actual kernel code determining the handling of requests is not implemented by the kernel class itself. Instead, there is the file logtalk_kernel_base_implementation.py which defines the class LogtalkKernelBaseImplementation. When the kernel is started, a (sub)object of this class is created. It handles the starting of and communication with the Logtalk server. For all requests (execution, shutdown, completion, inspection) the kernel receives, a LogtalkKernelBaseImplementation method is called. By creating a subclass of this and defining the path to it as kernel_implementation_path, the actual implementation code can be replaced. If no such path is defined, the path itself or the defined class is invalid, a default implementation is used instead.