avatarBert Gollnick

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5016

Abstract

edf">Wouldn’t it be great if the user could decide to work with light or dark mode? That is actually possible. All you need to do is specify the light and dark themes.</p> <figure id="30ce"> <div> <div>

            <iframe class="gist-iframe" src="/gist/SmartDataWithR/e5021d3636f481be0ce63347af27542b.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="e531">As a result you get a switch in the final document which allows to change between both.</p><figure id="725b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*V8KZHx4ywxlMjs1hb4y-WQ.gif"><figcaption>Light and Dark Mode (source: own image)</figcaption></figure><p id="03e2"><i>Reader Mode</i></p><p id="8691">Another nice feature is the <i>Reader Mode</i>. This allows the reader to focus on the content and hide all other visual sugar.</p><figure id="8712"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9ZhOG1CmJnlaokNI39sKPQ.gif"><figcaption>Reader Mode (source: own image)</figcaption></figure><p id="2e5b">To enable this you just have to set the flag.</p>
    <figure id="fd9b">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/e94ae6baa760feb278a7ab5ad0d8c464.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="aab1"><i>GitHub Link</i></p><p id="c718">It is possible to directly link (and a small icon) to the GitHub repository. You only need to reference your repo to the corresponding parameter.</p>
    <figure id="d165">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/2617e008077f0c95c2edb5a954c7e037.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><figure id="5813"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*GrAB6ZvAMyu1tW6kGgxk8w.png"><figcaption>GitHub repo link at left (source: own image)</figcaption></figure><p id="fe2a"><i>Footer</i></p><p id="d80f">Your page can provide additional information in the footer.</p><figure id="4d10"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5l0mY-kMH8QmMNxspMvVew.png"><figcaption>Footer (source: own image)</figcaption></figure><p id="4868">This is achieved with a few lines in <i>_quarto.yml</i>.</p>
    <figure id="5cd4">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/6a8b98f38e57a30919b7bf3820ce6b99.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="a153"><b>5. Markdown Features</b></p><p id="0cc2"><i>Code Chunks</i></p><p id="17bb">To me the most important feature are code chunks. This are areas of programming code, which are run during the rendering of the page. The code can be Python, R, or Julia. To create a chunk you need to wrap it in an area starting and ending with three back-ticks as shown here. Then you need to specify the language of the package. In the example you see R and Python.</p>
    <figure id="793a">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/1fa1d5ec97caaf1178fcf1d3cfe89eba.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="6673">You don’t need write the cumbersome code-chunk definition manually — instead you can use the shortcut CTRL + SHIFT + I.</p><p id="9a9d">This would just render the chunks and provide the result. The best is that you can keep the code as part of the document, so that readers can check out what produced the output. For this you have the option code_fold.</p>
    <figure id="6021">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/ed2cfa39c594f6345f68a4f8be9ce79a.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="564e">The rendered document will show “Code” and allows the user to expand it to see the actual code.</p><figure id="04e1"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*K2zaynY6KPLUXqvKwIwMbg.png"><figcaption>Code Chunk minimized (left) and expanded (right) (source: own image)</figcaption></figure><p id="d76a"><i>Tab Panels</i></p><p id="1c09">Tab Panels are a powerful feature when you want to organize information which is hierarchicall

Options

y on the same level. In my example I want to show how a functionality is implemented in Python, and then in R. With tab panels you can savel space is increase readability.</p><figure id="8905"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*UAKG2OajArWKewhDHDJYBw.png"><figcaption>Tab Sets (source: own image)</figcaption></figure><p id="6bad">To implement this, you can adapt the following template.</p> <figure id="6729"> <div> <div>

            <iframe class="gist-iframe" src="/gist/SmartDataWithR/1760eb4e3dd5c591e50d14539901e1b1.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="a38d"><i>Cross References</i></p><p id="69e1">If you want to cross reference to literature or other articles Quarto is a great support. After setup up your structure you are easily able to cross reference each bibliographic item at every point in your book. But you need to set your system up.</p><p id="c87a">First you need a reference in _quarto.yml to your bibliography file.</p>
    <figure id="eaaa">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/d52051f2e6ee07bbc6d38e7e319da37d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="3f5e">Next, you need your references file. I reference to my earlier article on Quarto.</p>
    <figure id="a92b">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/e45bcf5e314f9cb204ccb82f44a110f8.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="8afa">To create a cross reference you just need to place the @ character and a dropdown allows you to choose from different references you set up in references.bib.</p>
    <figure id="7c5a">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/SmartDataWithR/462920e70f4810d28f1afeaab589fcfc.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="22af">The rendered document shows a link which provides further information on hovering.</p><figure id="513d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*SwttCe4qk8Mj-yE88ufRcg.png"><figcaption>References (source: own image)</figcaption></figure><p id="27db">You typically want to show all references in a dedicated chapter. For this you need to install some additional program. Just run the following in a terminal.</p><div id="eebf"><pre>quarto <span class="hljs-keyword">install</span> tool tinytex</pre></div><p id="e5a1">Afterwards you need to restart your system. Next time you render your project you can see the complete list of references.</p><figure id="cfd4"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*B6i0F9AoalDDF6NFxNAS_w.png"><figcaption>References List (source: own image)</figcaption></figure><p id="7a9c"><b>6. Render Final Result</b></p><p id="d6d9">In this step you can create the output format of your choice. You only need to run in a terminal.</p><div id="df83"><pre><span class="hljs-attribute">quarto render</span></pre></div><p id="b438">The result is stored in a subfolder <i>_book</i>. If you want to render only to a specific format, e.g. PDF, you can set the <i>to-</i>parameter.</p><div id="9f77"><pre>quarto render <span class="hljs-comment">--to pdf</span></pre></div><p id="f632"><b>7. Publishing</b></p><p id="5232">You have static HTML content which you want to publish and make available to others. The are many options. I prefer to host the files on GitHub and deploy them with GitHub pages. My article “<a href="https://medium.com/@bert.gollnick/deploy-shiny-for-python-apps-with-github-pages-84cb8abbf7bf">Deploy Shiny for Python apps with GitHub pages</a>” guides you through the different steps.</p><p id="af62">Thanks for reading and your support,</p><p id="577c">Bert</p><div id="5b52" class="link-block">
      <a href="https://medium.com/@bert.gollnick/membership">
        <div>
          <div>
            <h2>Join Medium with my referral link - Bert Gollnick</h2>
            <div><h3>Read every story from Bert Gollnick (and thousands of other writers on Medium). Your membership fee directly supports…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*et61y5GijYrcN4hi)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Creating Websites, Blogs, or Books is now incredibly easy

Find out how you can create nicely structured content with Quarto

Photo by Lukas Blazek on Unsplash

Content

  1. Introduction
  2. Setup
  3. File Structure
  4. Project Features
  5. Markdown Features
  6. Render Final Result
  7. Publishing
  8. Introduction

Quarto is an open-source system to create reports, websites, blogs, and even books. In my first article “Create Interactive Reports with Python” I showed you how to create nice single page HTML-reports. Now I am going to show you how to write more complex documents like books that can be hosted online or even rendered to PDF.

You can find the source code on GitHub and the final deployed book here. By comparing the end-result and source code you can reverse-engineer how to implement features. In the following chapters I will guide you through the steps to set up your own book.

Final Result (source: own image)

2. Setup

  • You need to download Quarto CLI and install it on your system.
  • I work with Visual Studio code and recommend to install Quarto extension for VS code.

It is easiest to start with a few sample files, so from Command Palette (shortcut CTRL + SHIFT + P) you can select “Quarto: Create Project”.

Command Palette selection (source: own image)

Next, you have the choice to select the project type. In this article we develop a book, so please select “Book Project”.

Sample Project Types (source: own image)

Now you have to specify the working folder, in which the sample files are created.

I have adapted these files for this article and will explain in the next chapter what these files are used for.

Project Files (source: own image)

3. File Structure

  • The most important file which defines the look-and-feel of the final site or document is “_quarto.yml”. All the features described in the next chapter are set up here.
  • The different qmd-files (Quarto Markdown) like index.qmd, setup.qmd, references.qmd, and python_r.qmd are the files which will later be rendered. It is to me best practice to reserve at least one file for each chapter of the book. If you work with very long chapters, it could even be good to make more splits and have several documents per chapter.
  • If you have some experience with LaTex and created some documents with it, then you might be familiar with bib-files. references.bib is used for bibliographic citations. So if you cross reference you can specify the book or link you want to reference in the bib file and reference it from other chapters.
  • cover.png is an image that can be used in the book.

Let’s dive into the features.

4. Project Features

I distinguish between project features, which affect all pages and the general look, and markdown features, which affect a single page or paragraph.

All described project features are based on parameters defined in _quarto.yml. I will show the features and their corresponding parameters.

Theme

There is a range of different themes from which you can choose.

Theme “flatly” (source: own image)
Dark Mode Theme “darkly” (source: own image)

Wouldn’t it be great if the user could decide to work with light or dark mode? That is actually possible. All you need to do is specify the light and dark themes.

As a result you get a switch in the final document which allows to change between both.

Light and Dark Mode (source: own image)

Reader Mode

Another nice feature is the Reader Mode. This allows the reader to focus on the content and hide all other visual sugar.

Reader Mode (source: own image)

To enable this you just have to set the flag.

GitHub Link

It is possible to directly link (and a small icon) to the GitHub repository. You only need to reference your repo to the corresponding parameter.

GitHub repo link at left (source: own image)

Footer

Your page can provide additional information in the footer.

Footer (source: own image)

This is achieved with a few lines in _quarto.yml.

5. Markdown Features

Code Chunks

To me the most important feature are code chunks. This are areas of programming code, which are run during the rendering of the page. The code can be Python, R, or Julia. To create a chunk you need to wrap it in an area starting and ending with three back-ticks as shown here. Then you need to specify the language of the package. In the example you see R and Python.

You don’t need write the cumbersome code-chunk definition manually — instead you can use the shortcut CTRL + SHIFT + I.

This would just render the chunks and provide the result. The best is that you can keep the code as part of the document, so that readers can check out what produced the output. For this you have the option code_fold.

The rendered document will show “Code” and allows the user to expand it to see the actual code.

Code Chunk minimized (left) and expanded (right) (source: own image)

Tab Panels

Tab Panels are a powerful feature when you want to organize information which is hierarchically on the same level. In my example I want to show how a functionality is implemented in Python, and then in R. With tab panels you can savel space is increase readability.

Tab Sets (source: own image)

To implement this, you can adapt the following template.

Cross References

If you want to cross reference to literature or other articles Quarto is a great support. After setup up your structure you are easily able to cross reference each bibliographic item at every point in your book. But you need to set your system up.

First you need a reference in _quarto.yml to your bibliography file.

Next, you need your references file. I reference to my earlier article on Quarto.

To create a cross reference you just need to place the @ character and a dropdown allows you to choose from different references you set up in references.bib.

The rendered document shows a link which provides further information on hovering.

References (source: own image)

You typically want to show all references in a dedicated chapter. For this you need to install some additional program. Just run the following in a terminal.

quarto install tool tinytex

Afterwards you need to restart your system. Next time you render your project you can see the complete list of references.

References List (source: own image)

6. Render Final Result

In this step you can create the output format of your choice. You only need to run in a terminal.

quarto render

The result is stored in a subfolder _book. If you want to render only to a specific format, e.g. PDF, you can set the to-parameter.

quarto render --to pdf

7. Publishing

You have static HTML content which you want to publish and make available to others. The are many options. I prefer to host the files on GitHub and deploy them with GitHub pages. My article “Deploy Shiny for Python apps with GitHub pages” guides you through the different steps.

Thanks for reading and your support,

Bert

Web Development
Python
Quarto
Markdown
HTML
Recommended from ReadMedium