Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is custom.css in the generated html file from nbconvert?

I am using nbconvert to convert my jupyter notebook to html by

jupyter nbconvert my.ipynb --to html

Then it says:

[NbConvertApp] Converting notebook my.ipynb to html [NbConvertApp] Writing 407497 bytes to my.html

Then in the generated my.html, I can see it requires custom.css:

<!-- Custom stylesheet, it must be in the same directory as the html file -->
<link rel="stylesheet" href="custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS_HTML"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>

Info: nbconvert version is 5.6.1

Thanks

like image 201
derek Avatar asked Jun 26 '20 03:06

derek


People also ask

How do I export from Ipynb to HTML?

Download Jupyter Notebook as HTML Please click on File in the top navigation bar, and then Download as to see many options of download formats (PDF, HTML, Python, LaTeX). Please select HTML (. html) . The notebook application will start download automatically.

What is Jupyter Nbconvert?

The nbconvert tool, jupyter nbconvert , converts notebooks to various other formats via Jinja templates. The nbconvert tool allows you to convert an . ipynb notebook file into various static formats including: HTML. LaTeX.


1 Answers

If your sole problem is this line: <link rel="stylesheet" href="custom.css">, you can modify the template file and write a custom HTML exporter:

Navigate to the source directory of nbconvert. Go to ./templates/html/. There will be a file called full.tpl. This serves as the template for your HTML file. Open the file with a text editor and delete the <link rel="stylesheet" href="custom.css"> line. Save the file with a .tpl extension (fullcustom.tpl). Make sure you save it in the same directory as full.tpl.

The next step is to either write a custom exporter class in Python(recommended). You have to inherit the HTMLExporter class defined in ./exporters/html.py. You can follow the procedure described here: https://nbconvert.readthedocs.io/en/latest/external_exporters.html

Now, if you do not want to spend the time doing this, a quick "kludge" would be to actually modify the HTMLExporter class. Create a copy of the html.py file if you want as it is being modified. Open ./exporters/html.py and find the following method in the HTMLExporter class:

@default('template_file')
    def _template_file_default(self):
        return 'full.tpl'

Change return full.tpl to the name you saved your modified version of full.tpl with (fullcustom.tpl).

like image 50
Amal K Avatar answered Sep 19 '22 16:09

Amal K