Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Theresa Pollinger
MoSIS
Commits
c7baea82
Commit
c7baea82
authored
Mar 08, 2018
by
Theresa Pollinger
Browse files
playing around with html and matplotlib contents
parent
3473cb6f
Changes
2
Hide whitespace changes
Inline
Side-by-side
interview_kernel/interview_kernel.py
View file @
c7baea82
#!/usr/bin/env python3
import
sys
from
ipykernel.kernelbase
import
Kernel
from
ipykernel.comm
import
CommManager
from
metakernel
import
MetaKernel
from
IPython.display
import
HTML
,
Javascript
from
metakernel
import
IPythonKernel
# http://mattoc.com/python-yes-no-prompt-cli.html
# https://github.com/phfaist/pylatexenc for directly converting Latex commands to unicode
from
pylatexenc.latex2text
import
LatexNodes2Text
import
getpass
from
pde_state_machine
import
*
import
matplotlib.pyplot
as
plt
...
...
@@ -47,11 +43,13 @@ class Interview(MetaKernel):
# call superclass constructor
super
(
Interview
,
self
).
__init__
(
**
kwargs
)
# cf. https://github.com/ipython/ipykernel/blob/7d91110f8f471dbdd3ea65099abcb96a99179557/ipykernel/ipkernel.py
# self.comm_manager = CommManager(parent=self, kernel=self)
from
IPython
import
get_ipython
from
metakernel
import
register_ipython_magics
self
.
do_execute
(
"%matplotlib nbagg"
)
plt
.
ion
()
# To make custom magics happen, cf. https://github.com/Calysto/metakernel
# from IPython import get_ipython
# from metakernel import register_ipython_magics
# register_ipython_magics()
self
.
update_prompt
()
...
...
@@ -68,6 +66,7 @@ class Interview(MetaKernel):
# allow_stdin=False):
def
do_execute_direct
(
self
,
code
,
silent
=
False
):
"""This is where the user input enters our code"""
arg
=
LatexNodes2Text
().
latex_to_text
(
code
)
if
not
self
.
keyword_handling
(
arg
):
...
...
@@ -132,6 +131,12 @@ class Interview(MetaKernel):
if
arg
.
startswith
(
"recap"
):
self
.
state_machine
.
recap
(
arg
)
return
True
if
arg
.
startswith
(
"html"
):
self
.
display_html
()
return
True
if
arg
.
startswith
(
"plt"
):
self
.
display_plt
()
return
True
return
False
# called when user types 'explain [expression]'
...
...
@@ -151,7 +156,7 @@ class Interview(MetaKernel):
# called when user types 'undo'
def
do_undo
(
self
,
expression
):
"Go back to the last question"
self
.
trigger
(
'last_state'
)
self
.
state_machine
.
trigger
(
'last_state'
)
def
help_undo
(
self
):
self
.
poutput
(
'
\n
'
.
join
([
'undo'
,
...
...
@@ -161,6 +166,11 @@ class Interview(MetaKernel):
def
update_prompt
(
self
):
self
.
prompt
=
"("
+
self
.
state_machine
.
state
+
")"
#TODO
def
do_shutdown
(
self
,
restart
):
self
.
state_machine
.
mmtinterface
.
exit_mmt
()
return
super
(
Interview
,
self
).
do_shutdown
(
restart
)
# tab completion for empty lines
def
completenames
(
self
,
text
,
line
,
begidx
,
endidx
):
"""Override of cmd2 method which completes command names both for command completion and help."""
...
...
@@ -188,6 +198,37 @@ class Interview(MetaKernel):
cmd_completion
[
0
]
+=
' '
return
cmd_completion
def
display_html
(
self
):
self
.
Display
(
HTML
(
"""
<style type="text/css">
.styled-background { background-color: #ff7; }
</style>
<script>
if (typeof markedText !== 'undefined') {
markedText.clear();
}
IPython.notebook.select_prev()
var cell = IPython.notebook.get_selected_cell();
markedText = cell.code_mirror.markText({line: %s, col: %s},
{line: %s, col: %s},
{className: "styled-background"});
cell.show_line_numbers(1)
IPython.notebook.select_next()
</script>
"""
%
(
1
,
0
,
3
,
0
)))
othercode
=
"""
<iframe>
### have tgview here
</iframe>
"""
def
display_plt
(
self
):
# plt.ion()
# matplotlib.use('nbagg')
self
.
Display
(
plt
.
plot
([
3
,
8
,
2
,
5
,
1
]))
# plt.show() #TODO find out why there is no comm and interactive shell - and if it should be there
if
__name__
==
'__main__'
:
# from ipykernel.kernelapp import IPKernelApp
...
...
interview_kernel/pde_state_machine.py
View file @
c7baea82
...
...
@@ -796,64 +796,12 @@ class PDE_States:
self
.
poutput
(
"Generated ExaStencils input."
)
# TODO generate and run simulation
# display solution using matplotlib
# cf.https://github.com/ipython/ipython/blob/6a5220d032f5a60267b051783721aa0d5a0e3373/IPython/core/magics/pylab.py
import
matplotlib
matplotlib
.
use
(
'nbagg'
)
#
matplotlib.use('nbagg')
import
matplotlib.pyplot
as
plt
#from ipykernel.core import InteractiveShell
plt
.
ion
()
#try:
# gui, backend = self.shell.enable_matplotlib("nbagg")
#except Exception as e:
# raise InterviewError("Unable to use matplotlib for display, sorry! \n" + e.args[0])
plt
.
plot
([
1
,
2
,
3
])
def
enable_matplotlib
(
self
,
gui
=
None
):
"""Enable interactive matplotlib and inline figure support.
stolen from
https://github.com/ipython/ipython/blob/35a4f43a1bbcd371d6f917e6a2c258848df6e47d/IPython/core/interactiveshell.py .
This takes the following steps:
1. select the appropriate eventloop and matplotlib backend
2. set up matplotlib for interactive use with that backend
3. configure formatters for inline figure display
4. enable the selected gui eventloop
Parameters
----------
gui : optional, string
If given, dictates the choice of matplotlib GUI backend to use
(should be one of IPython's supported backends, 'qt', 'osx', 'tk',
'gtk', 'wx' or 'inline'), otherwise we use the default chosen by
matplotlib (as dictated by the matplotlib build-time options plus the
user's matplotlibrc configuration file). Note that not all backends
make sense in all contexts, for example a terminal ipython can't
display figures inline.
"""
from
IPython.core
import
pylabtools
as
pt
gui
,
backend
=
pt
.
find_gui_and_backend
(
gui
,
self
.
pylab_gui_select
)
if
gui
!=
'inline'
:
# If we have our first gui selection, store it
if
self
.
pylab_gui_select
is
None
:
self
.
pylab_gui_select
=
gui
# Otherwise if they are different
elif
gui
!=
self
.
pylab_gui_select
:
print
(
'Warning: Cannot change to a different GUI toolkit: %s.'
' Using %s instead.'
%
(
gui
,
self
.
pylab_gui_select
))
gui
,
backend
=
pt
.
find_gui_and_backend
(
self
.
pylab_gui_select
)
pt
.
activate_matplotlib
(
backend
)
pt
.
configure_inline_support
(
self
,
backend
)
# Now we must activate the gui pylab wants to use, and fix %run to take
# plot updates into account
self
.
enable_gui
(
gui
)
self
.
magics_manager
.
registry
[
'ExecutionMagics'
].
default_runner
=
\
pt
.
mpl_runner
(
self
.
safe_execfile
)
return
gui
,
backend
def
sim_ok_fd
(
self
):
self
.
simdata
[
"sim"
][
"type"
]
=
"FiniteDifferences"
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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