Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Lifeware
biocham
Commits
3473cf87
Commit
3473cf87
authored
Feb 17, 2016
by
SOLIMAN Sylvain
Browse files
display images (untested)
parent
1ca053b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
biocham_kernel/images.py
0 → 100644
View file @
3473cf87
# very much inspired form the example bash_kernel
# https://raw.githubusercontent.com/takluyver/bash_kernel/
import
base64
import
imghdr
import
os
def
display_data_for_image
(
filename
):
with
open
(
filename
,
'rb'
)
as
f
:
image
=
f
.
read
()
os
.
unlink
(
filename
)
image_type
=
imghdr
.
what
(
None
,
image
)
if
image_type
is
None
:
raise
ValueError
(
"Not a valid image: %s"
%
image
)
image_data
=
base64
.
b64encode
(
image
).
decode
(
'ascii'
)
content
=
{
'data'
:
{
'image/'
+
image_type
:
image_data
},
'metadata'
:
{}
}
return
content
def
extract_image_filenames
(
output
):
output_lines
=
[]
image_filenames
=
[]
for
line
in
output
.
split
(
"
\n
"
):
if
line
.
startswith
(
'<img src="'
):
filename
=
line
.
rstrip
().
split
(
'"'
)[
1
]
image_filenames
.
append
(
filename
)
else
:
output_lines
.
append
(
line
)
output
=
"
\n
"
.
join
(
output_lines
)
return
image_filenames
,
output
biocham_kernel/kernel.py
View file @
3473cf87
...
...
@@ -3,6 +3,8 @@ from subprocess import check_output
from
ipykernel.kernelbase
import
Kernel
from
pexpect
import
replwrap
from
.images
import
extract_image_filenames
,
display_data_for_image
class
BiochamKernel
(
Kernel
):
implementation
=
'Biocham'
...
...
@@ -20,19 +22,32 @@ class BiochamKernel(Kernel):
Kernel
.
__init__
(
self
,
**
kwargs
)
self
.
_start_biocham
()
def
_start_biocham
(
self
):
self
.
biocham
=
replwrap
.
REPLWrapper
(
'biocham4 --jupyter'
,
'biocham: '
,
None
)
self
.
biocham
=
replwrap
.
REPLWrapper
(
'biocham4 --jupyter'
,
'biocham: '
,
None
)
def
do_execute
(
self
,
code
,
silent
,
store_history
=
True
,
user_expressions
=
None
,
allow_stdin
=
False
):
output
=
self
.
biocham
.
run_command
(
code
)
image_filenames
,
output
=
extract_image_filenames
(
output
)
if
not
silent
:
stream_content
=
{
'name'
:
'stdout'
,
'text'
:
output
}
self
.
send_response
(
self
.
iopub_socket
,
'stream'
,
stream_content
)
for
filename
in
image_filenames
:
try
:
data
=
display_data_for_image
(
filename
)
except
ValueError
as
e
:
message
=
{
'name'
:
'stdout'
,
'text'
:
str
(
e
)}
self
.
send_response
(
self
.
iopub_socket
,
'stream'
,
message
)
else
:
self
.
send_response
(
self
.
iopub_socket
,
'display_data'
,
data
)
return
{
'status'
:
'ok'
,
# The base class increments the execution count
'execution_count'
:
self
.
execution_count
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment