kts / ipostscript Goto Github PK
View Code? Open in Web Editor NEWjupyter kernel for Postscript (ghostscript)
jupyter kernel for Postscript (ghostscript)
from ipykernel.kernelbase import Kernel
from subprocess import check_output
import os.path
import re
import signal
from pexpect import popen_spawn
import pexpect
import platform
def iswindows():
return platform.system() == "Windows"
__version__ = "0.1.0"
class GSWrapper:
"""
Wrap Ghostscript executable using pexpect
"""
def __init__(self):
"""
todo: config cmd can be 'gs', 'gs-X11', etc.
"""
if iswindows():
self.cmd = 'gswin64c -dNOSAFER'
self.p = popen_spawn.PopenSpawn(self.cmd)
else:
self.cmd = 'gs -dNOSAFER'
self.p = pexpect.spawn(self.cmd)
# maybe input/output to gs is always 'ascii'?
self.encoding = "utf8"
pattern1 = b"GS>"
self.p.expect(re.compile(pattern1))
assert(self.p.after == b"GS>")
def run(self, code):
"""
Execute 'code' in session.
newline is appended
"""
prompt = re.compile(b'GS(>|<\d+>)')
code = code + "\n"
self.p.send(code)
self.p.expect(prompt)
before = self.p.before.decode(self.encoding)
after = self.p.after.decode(self.encoding)
if not iswindows():
code = code.strip()
before = before[len(code):]
if len(before)==0:
r = after
else:
r = before + after
# .after is always the prompt itself. ?
# eg: b'GS>'
#
# could parse to get stack size
# 'GS>', 'GS<1>', ...
#
return r.strip()
Note that I have added the -dNOSAFER option to cmd to allow scripts to run other scripts.
e.g.
(utils.ps) run
Hi,
Thanks for the kernel.
I made some minimal changes to add windows support as pexpect.spawn()
doesn't exist on windows.
No changes were made to the Linux functionality.
Only requirement is that gswin64c.exe is on the path.
from pexpect import popen_spawn
import pexpect
import platform
def iswindows():
return platform.system() == "Windows"
__version__ = "0.1.0"
class GSWrapper:
"""
Wrap Ghostscript executable using pexpect
"""
def __init__(self,
cmd = 'gs'
):
"""
todo: config cmd can be 'gs', 'gs-X11', etc.
"""
if iswindows():
cmd = 'gswin64c.exe -dNOSAFER'
self.p = popen_spawn.PopenSpawn(cmd)
else:
self.p = pexpect.spawn(cmd)
# maybe input/output to gs is always 'ascii'?
self.encoding = "utf8"
pattern1 = b"GS>"
self.p.expect(re.compile(pattern1))
assert(self.p.after == b"GS>")
def run(self, code):
"""
Execute 'code' in session.
newline is appended
"""
rx = re.compile(b'GS(>|<\d+>)')
cc = code + "\n"
# sendline uses "\r\n"?
#p.sendline(c)
self.p.send(cc)
self.p.expect(rx)
c1 = self.p.before.decode(self.encoding)
code1 = code.strip()
if iswindows():
r = c1
else:
assert(c1.startswith(code1))
r = c1[len(code1):]
# .after is always the prompt itself. ?
# eg: b'GS>'
#
# could parse to get stack size
# 'GS>', 'GS<1>', ...
#
return r.strip()
example of it running in a jupyter console - python 3.11.5, Windows 10 x64,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.