Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Merge acb0258 into 3a87e26
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed May 14, 2019
2 parents 3a87e26 + acb0258 commit 8c5452b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
61 changes: 61 additions & 0 deletions examples/example_bash.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Bash example\n",
"\n",
"\n",
"This example notebook uses a Bash kernel.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# A basic hello, world in Bash\n",
"\n",
"function hello {\n",
" echo hello, world\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello, world\n"
]
}
],
"source": [
"hello"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Bash",
"language": "bash",
"name": "bash"
},
"language_info": {
"codemirror_mode": "shell",
"file_extension": ".sh",
"mimetype": "text/x-sh",
"name": "bash"
},
"thumbnail_figure": "images/bash.png"
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added examples/images/bash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 17 additions & 10 deletions sphinx_nbexamples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class NotebookProcessor(object):
.. container:: sphx-glr-download
**Download python file:** :download:`{pyfile}`
**Download {language} script:** :download:`{script}`
**Download IPython notebook:** :download:`{nbfile}`
"""
Expand Down Expand Up @@ -359,7 +359,9 @@ def process_notebook(self, disable_warnings=True):
if disable_warnings:
nb.cells.pop(i)

self.py_file = self.get_out_file('py')
language_info = getattr(nb.metadata, 'language_info', {})
ext = language_info.get('file_extension', 'py')
self.script = self.get_out_file(ext.lstrip('.'))

if self.remove_tags:
tp = nbconvert.preprocessors.TagRemovePreprocessor(timeout=300)
Expand Down Expand Up @@ -415,15 +417,18 @@ def create_rst(self, nb, in_dir, odir):
rst_content = raw_rst
rst_content = '.. _%s:\n\n' % self.reference + \
rst_content
language_info = getattr(nb.metadata, 'language_info', {})
url = self.url
if url is not None:
rst_content += self.CODE_DOWNLOAD_NBVIEWER.format(
pyfile=os.path.basename(self.py_file),
language=language_info.get('name', 'Python'),
script=os.path.basename(self.script),
nbfile=os.path.basename(self.outfile),
url=url)
else:
rst_content += self.CODE_DOWNLOAD.format(
pyfile=os.path.basename(self.py_file),
language=language_info.get('name', 'Python'),
script=os.path.basename(self.script),
nbfile=os.path.basename(self.outfile))
supplementary_files = self.supplementary_files
other_supplementary_files = self.other_supplementary_files
Expand Down Expand Up @@ -466,22 +471,24 @@ def create_py(self, nb, force=False):
# some weird like '[0;31mOut[5]: ' which look like
# color information if we allow the call of nbconvert.export_python
if list(map(int, re.findall('\d+', nbconvert.__version__))) >= [4, 2]:
py_file = os.path.basename(self.py_file)
script = os.path.basename(self.script)
else:
py_file = self.py_file
script = self.script
try:
level = logger.logger.level
except AttributeError:
level = logger.level
spr.call(['jupyter', 'nbconvert', '--to=python',
'--output=' + py_file, '--log-level=%s' % level,
spr.call(['jupyter', 'nbconvert', '--to=script',
'--output=' + os.path.splitext(script)[0], '--log-level=%s' % level,
self.outfile])
with open(self.py_file) as f:
if not script.endswith('.py'):
return
with open(self.script) as f:
py_content = f.read()
# comment out ipython magics
py_content = re.sub('^\s*get_ipython\(\).magic.*', '# \g<0>',
py_content, flags=re.MULTILINE)
with open(self.py_file, 'w') as f:
with open(self.script, 'w') as f:
f.write(py_content)

def data_download(self, files):
Expand Down

0 comments on commit 8c5452b

Please sign in to comment.