This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/python/yap_kernel/yap_ipython/prolog/jupyter.yap

148 lines
3.1 KiB
Plaintext
Raw Normal View History

2018-06-03 23:59:17 +01:00
/**
2018-05-28 09:31:59 +01:00
* @file jupyter.yap4py
*
* @brief JUpyter support.
*/
2017-08-21 12:36:48 +01:00
2018-06-03 12:07:38 +01:00
2018-03-12 15:11:59 +00:00
% :- module( jupyter,
% [jupyter_query/3,
% errors/2,
% ready/2,
% completion/2,
% ]
%% ).
2018-05-28 09:31:59 +01:00
:- use_module(library(hacks)).
2018-01-18 14:47:27 +00:00
:- use_module(library(lists)).
:- use_module(library(maplist)).
2018-05-28 09:31:59 +01:00
2018-01-18 14:47:27 +00:00
:- use_module(library(python)).
2018-05-28 09:31:59 +01:00
:- use_module(library(yapi)).
2018-06-02 23:04:51 +01:00
:- use_module(library(complete)).
2018-01-05 16:57:38 +00:00
2018-01-18 14:47:27 +00:00
:- python_import(sys).
2018-01-05 16:57:38 +00:00
2018-07-09 00:50:00 +01:00
jupyter_query(Caller, Prog, Query ) :-
catch(
jupyter_cell(Caller, Prog, Query),
E,
'$Error'(E, top)
).
2017-12-14 18:40:22 +00:00
2018-06-05 11:20:39 +01:00
jupyter_cell(_Caller, Cell, _Line) :-
2018-03-12 15:11:59 +00:00
jupyter_consult(Cell), %stack_dump,
2017-12-14 18:40:22 +00:00
fail.
2018-06-05 11:20:39 +01:00
jupyter_cell( _Caller, _, '' ) :- !.
2018-03-02 21:18:24 +00:00
jupyter_cell( _Caller, _, Line ) :-
2017-12-20 00:29:15 +00:00
blank( Line ),
!.
2018-07-09 00:50:00 +01:00
jupyter_cell( Self, _, Line ) :-
%Self := Caller.query,
2018-06-01 13:22:13 +01:00
python_query(Self,Line).
2018-06-01 08:37:25 +01:00
2018-06-01 13:22:13 +01:00
restreams(call) :-
2018-06-01 08:37:25 +01:00
streams(true).
restreams(fail) :-
streams(false).
2018-06-01 13:22:13 +01:00
restreams(answer).
2018-06-01 08:37:25 +01:00
restreams(exit) :-
streams(false).
2018-06-01 13:22:13 +01:00
restreams(!).
restreams(external_exception(_)).
restreams(exception).
2017-08-21 12:36:48 +01:00
2017-12-20 00:29:15 +00:00
jupyter_consult(Text) :-
blank( Text ),
!.
jupyter_consult(Cell) :-
2018-06-03 12:07:38 +01:00
open_mem_read_stream( Cell, Stream),
2018-06-05 11:20:39 +01:00
load_files(user:'jupyter cell',[stream(Stream)]).
2017-12-20 00:29:15 +00:00
2018-07-09 00:50:00 +01:00
blank(Text) :-
atom(Text),
!,
atom_codes(Text, L),
maplist( code_type(space), L).
blank(Text) :-
string(Text),
!,
string_codes(Text, L),
maplist( code_type(space), L).
streams(false) :-
nb_setval(jupyter_cell, false),
close(user_input),
close(user_output),
close(user_error).
2018-03-12 15:11:59 +00:00
streams(true) :-
2018-06-01 08:37:25 +01:00
nb_setval(jupyter_cell, true),
2018-07-09 00:50:00 +01:00
open('/python/input', read, _Input, [alias(user_input),bom(false),script(false)]),
open('/python/sys.stdout', append, _Output, [alias(user_output)]),
open('/python/sys.stderr', append, _Error, [alias(user_error)]).
ready(Self, Cell, P, Q ) :-
catch(
all_clear(Self, Cell, P, Q)
E,
system_error(error,E).
all_clear( Self, _Cell, P, Q) :-
no_errors( Self, P ),
yap_flag(singleton_variables, Old, false)
no_errors( Self, Q ).
no_errors( _Self, Text ) :-
blank(Text),
no_errors( Self, Text ) :-
2018-01-18 14:47:27 +00:00
setup_call_cleanup(
2018-07-09 00:50:00 +01:00
open_esh( Self, Text, Stream),
esh(Self, Stream),
close_esh( Self, Stream )
2018-01-18 14:47:27 +00:00
).
2018-07-09 00:50:00 +01:00
esh(Self, Stream) :-
2018-01-18 14:47:27 +00:00
repeat,
2018-07-09 00:50:00 +01:00
catch(
read_clause(Stream, Cl, [term_position(_Pos), syntax_errors(fail)] ),
Error,
syntax(Self, Error)
),
2018-02-25 00:29:08 +00:00
Cl == end_of_file,
!.
2018-01-18 14:47:27 +00:00
2018-07-09 00:50:00 +01:00
syntax(_Self, E) :- writeln(user_error, E), fail.
syntax(Self, error(syntax_error(Cause),info(between(_,LN,_), _FileName, CharPos, Details))) :-
Self.errors := [t(Cause,LN,CharPos,Details)] + Self.errors,
!.
syntax(_Self, E) :- throw(E).
open_esh(Self, Text, Stream) :-
2018-02-24 14:39:17 +00:00
Self.errors := [],
open_mem_read_stream( Text, Stream ).
2018-01-18 14:47:27 +00:00
2018-02-24 14:39:17 +00:00
:- initialization( nb_setval( jupyter, off ) ).
2018-03-02 21:18:24 +00:00
2018-07-09 00:50:00 +01:00
close_esh( _Self, Stream ) :-
close(Stream).
2018-05-21 14:45:24 +01:00
:- if( current_prolog_flag(apple, true) ).
:- putenv( 'LC_ALL', 'en_us:UTF-8').
plot_inline :-
X := self.inline_plotting,
nb_setval(inline, X ),
X = true,
!,
:= (
import( matplotlib ),
matplotlib.use( `nbagg` )
).
:- endif.
2018-03-02 21:18:24 +00:00
%:- ( start_low_level_trace ).