Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2af1087

Browse files
committed
robust css/preamble path finding despite symlinks
I have a `~/bin/nbconvert` symlink to ~/code/nbconvert/nbconvert.py so that I can call it from anywhere in the filesystem. Stylesheets were found previously using `__file__`, which can change depending on where one puts symlinks. Additionally, I found one other place where such a situation was anticipated, but the solution was using a bit too much code. The simplest way to get around this situation is to used `os.path.realpath`, which is what I've done here. Here's a traceback of the error, in case someone stumbles into this. $ nbconvert -f html test_display.ipynb Traceback (most recent call last): File "/home/pi/bin/nbconvert", line 1490, in <module> main(infile=args.infile[0], format=args.format) File "/home/pi/bin/nbconvert", line 1459, in main htmlfname = converter.render() File "/home/pi/bin/nbconvert", line 311, in render self.output = self.convert() File "/home/pi/bin/nbconvert", line 293, in convert lines.extend(self.optional_header()) File "/home/pi/bin/nbconvert", line 808, in optional_header return ['<html>', '<head>'] + self.header_body() + \ File "/home/pi/bin/nbconvert", line 790, in header_body header.extend(self._stylesheet(sheet)) File "/home/pi/bin/nbconvert", line 756, in _stylesheet with io.open(fname, encoding='utf-8') as f: IOError: [Errno 2] No such file or directory: '/home/pi/bin/css/static_html.css'
1 parent 39aa8a9 commit 2af1087

1 file changed

Lines changed: 2 additions & 3 deletions

File tree

nbconvert.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def header_body(self):
774774
static = os.path.join(path.get_ipython_package_dir(),
775775
'frontend', 'html', 'notebook', 'static',
776776
)
777-
here = os.path.split(os.path.abspath(__file__))[0]
777+
here = os.path.split(os.path.realpath(__file__))[0]
778778
css = os.path.join(static, 'css')
779779
for sheet in [
780780
# do we need jquery and prettify?
@@ -1014,8 +1014,7 @@ def convert(self):
10141014
]
10151015
# Load our own preamble, which is stored next to the main file. We
10161016
# need to be careful in case the script entry point is a symlink
1017-
myfile = __file__ if not os.path.islink(__file__) else \
1018-
os.readlink(__file__)
1017+
myfile = os.path.realpath(__file__)
10191018
with open(os.path.join(os.path.dirname(myfile), 'preamble.tex')) as f:
10201019
final.append(f.read())
10211020

0 commit comments

Comments
 (0)