Quarto, Python, and VS Code: Quarto Reports In VS Code

Reading time:
time
min

Quarto, the successor of R Markdown, is a powerful tool for creating reports and presentations. In this blog post, I’ll show you how easy it is to use Quarto in <a href="https://code.visualstudio.com/" target="_blank" rel="noopener">Visual Studio Code</a> (VS Code) together with Python. Make your coding experience a breeze and learn useful techniques with Quarto. In an earlier post, we discussed what quarto is capable of when it comes to working with R in RStudio. But that’s only one part of the data science and machine learning world. The other big player - is Python. <blockquote>Need help with RStudio (Posit) data science products for your project? Appsilon can help with <a href="https://appsilon.com/rstudio-certified-partner/" target="_blank" rel="noopener">end-to-end service as a Full Service RStudio Certified Partner and product reseller</a>.</blockquote> And as RStudio (<a href="https://appsilon.com/posit-rstudio-rebrands/" target="_blank" rel="noopener">Posit</a>) pushes forward with more language agnostics products, it's never been a better time to learn how their data science products can interact with other tools. Table of contents: <ul><li><a href="#quarto-support">Quarto support in VS Code</a></li><li><a href="#python-basics">Python basics in Quarto</a></li><li><a href="#quarto-vscode">Working with Quarto files live in VS Code</a></li><li><a href="#qmd">Rendering Python qmd files with Quarto</a></li><li><a href="#example">Example of Quarto report with Python</a></li><li><a href="#workflow">Suggested workflow for Quarto in VS Code</a></li></ul> <hr /> <h2 id="quarto-support">Quarto support in VS Code</h2> First, you’ll need to install the tool. You can do that by following the steps for installing in our R Quarto tutorial. Second, you’ll need to install the Quarto extension for VS Code. This can be done by following <a href="https://quarto.org/docs/get-started/hello/vscode.html" target="_blank" rel="noopener">the official Quarto tutorial</a>. Now everything is set up and ready for experimenting and reporting! Next, let’s explain the ‘why’ before you invest any more time.  <h3>Why would I bother with Quarto in VS Code?</h3> It’s fair to say that RStudio is the go-to, gold standard IDE for R programmers. However, that’s not the case with Python. Most data scientists favoring Python tend to use other options like VS Code.  FYI - are you using RStudio Workbench? Explore <a href="https://solutions.rstudio.com/python/" target="_blank" rel="noopener">RStudio's (Posit) solutions for developing Python</a> with RStudio IDE, VS Code, Jupyter Notebook, and more.  For those of you entrenched in your preferred editors, it might be easier to use Quarto in VS Code than in RStudio IDE. You can keep all of your favorite colors’ themes, configurations, and shortcuts across programming in different languages to make life simpler. Of course, Quarto in VS Code is still Quarto, so the overall capabilities are the same. There is nothing wrong with <a href="https://www.rstudio.com/blog/why-rstudio-supports-python/" target="_blank" rel="noopener">running Python in RStudio</a>. Even more so, we encourage you to experiment with it if you have experience in using RStudio. This can also be particularly helpful if you work with distributed teams and need a one-stop shop for distributed teams like RStudio (Posit) Connect. <h2 id="python-basics">Python basics in Quarto and VS Code</h2> <h3 id="quarto-vscode">Working with Quarto files live in VS Code</h3> To begin, .qmd files can be used the same way as the <a href="https://code.visualstudio.com/docs/python/jupyter-support-py" target="_blank" rel="noopener">.py files with ##% in VS Code</a> when it comes to running them line-by-line. The only difference is that you write code in chunks like: <img class="size-full wp-image-14966 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01da8db0362a991a70657_python-debug-quarto.webp" alt="qmd file python debug quarto" width="424" height="112" /> instead of: <img class="alignnone size-full wp-image-14964 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01da9051092b3b542a84d_hello-world-quarto.webp" alt=".py file python debug quarto" width="423" height="112" /> The VS Code magic trick to pick <a href="https://code.visualstudio.com/docs/python/environments#_python-environments" target="_blank" rel="noopener">any python from the installed environments</a> as an interpreter for the current file works wonders. Other tricks like running cells out of order, although not recommended, also work without any problems. <blockquote>New to Python and prefer working in R? Learn <a href="https://appsilon.com/use-r-and-python-together/" target="_blank" rel="noopener">how to use R and Python together</a>!</blockquote> What positively surprised me was the out-of-the-box support of shortcuts like <code>ctrl+shift+I</code> for chunk creation that I was used to from RStudio. The autosuggestions for Quarto parameters were also genuinely useful! <img class="alignnone size-full wp-image-14977" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01dab99f568749835b33e_quarto_images_quarto_inteli-reduced.gif" alt="Quarto run cell shortcuts" width="950" height="576" /> <h3 id="qmd">Rendering Python qmd files with Quarto</h3> You'll rarely use Quarto just to run lines out of order. It’s all about creating beautiful, interactive reports! First, you must specify the Jupyter kernel that should run the code in the document yaml. For example, to choose the "chosen-kernel-name" kernel, you should write: <pre><code>title:"Example qmd file" jupyter:"chosen-kernal-name"</code></pre> This means that you must <a href="https://ipython.readthedocs.io/en/stable/install/kernel_install.html" target="_blank" rel="noopener">register your Python/Conda environment to jupyter</a> before you render the document. Then it’s as easy as running Quarto: Render command from VS Code ctrl+shift+P panel! <img class="alignnone size-full wp-image-14968 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01dac687eb4992e5730ed_quarto-render.webp" alt="quarto render command from vs code ctrl+shift+p" width="1204" height="278" /> Using the latest versions of Quarto (1.0.35) and Quarto VS Code Extension (1.28.0) I wasn’t able to render .ipynb files this way and I had to rely on the CLI version of Quarto. This will likely be addressed in upcoming versions. Some might remember that the R Markdown format had an issue with forgetting the Python variables between code chunks so each chunk had to be a separate program, so to say. That’s no longer a problem using Quarto! <h3 id="example">Example of Quarto report with Python</h3> Quarto demo <pre><img class="alignnone size-full wp-image-15017" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01dae817460dff135f0f5_quarto_vscode-reduced.gif" alt="Quarto VSCode demo" width="1006" height="622" /></pre> Let’s briefly explore a few distilled features of Quarto. Isn’t all this interactivity and linkability amazing? The code you want to show is visible but imports are not. The auto-generated table of contents is also here! The above demo has been rendered using the following code: <pre><code>--- title: "Quarto report" jupyter: "python310-ds" format:  html:    theme:          light: flatly          dark: darkly    toc: true --- <br>My first Python in Quarto report! <br>## Data loading <br>Table @tbl-iris presents data on the Iris dataset. <br>```{python} #| echo: false import pandas as pd import matplotlib.pyplot as plt ``` <br> ```{python} #| code-fold: true #| tbl-cap: The Iris dataset #| label: tbl-iris df = pd.read_csv("data/iris.csv") df ``` <br>## Plots <br>```{python} #| label: fig-variety-histogram #| fig-cap: Histogram of sepal lengths #| fig-align: center #| code-fold: true df.groupby("variety")["sepal.length"].hist(alpha=0.5, legend=True) plt.show() ``` <br>Cool! We've created @fig-variety-histogram to learn more about iris sepal lengths distribution, using data from @tbl-iris. </code></pre> As you can see, everything is quite simple and uses the exact same Quarto syntax as the reports in R. <h3 id="workflow">Suggested workflow for Quarto in VS Code</h3> The feature of re-rerendering the report every time you save is impressive, handy, and often desired by users. But this isn’t always the case. For example, if you work with large reports that involve heavy computation, it might actually be suboptimal.  You might try using <a href="https://quarto.org/docs/projects/code-execution.html#cache" target="_blank" rel="noopener">Quarto cache</a>. Unfortunately, at the time of writing this post, this feature is not fully supported. In such a case, you can try using Jupyter notebooks instead of qmd files (they work with Quarto as well!) or work in an interactive way within VS Code and render the report presentation after some time. <h2>Closing notes on Quarto with Python in VS Code</h2> Even without using the intricate features of Quarto, the reports generated using it are just beautiful. But those additional features are available, and you can absolutely make your reports top of the line!  Everything can be interactive with <a href="https://quarto.org/docs/get-started/computations/text-editor.html" target="_blank" rel="noopener">plotly plots</a> embedded. From the moment I discovered Quarto, I’ve begun using it every time I have to present some insights from data! Although I have to say, I prefer to use Quarto with Jupyter notebooks instead of qmd files. But don’t worry they will be covered in the next post! <blockquote>Py/Shiny (Shiny for Python) is still in beta. Until then, check out <a href="https://appsilon.com/dash-vs-shiny/" target="_blank" rel="noopener">Python Dash vs R Shiny to compare dashboards for your project reporting</a>.</blockquote> Not all of the features are fully supported yet. But I’m sure they’ll begin rolling out after the official release of Quarto. So I’ll be keeping my eyes peeled for updates on RStudio’s blog! <h3>Environment</h3> This post was written under: <ul><li>Quarto <code>quarto-1.0.35-linux-amd64</code></li><li>Quarto VS Code extension <code>v1.28.0</code></li></ul> Quarto is a quickly developing software, you may not encounter some problems that I did! Or you may encounter other issues. Let us know in the comments below, if you ran into anything puzzling and how you resolved it.

Have questions or insights?

Engage with experts, share ideas and take your data journey to the next level!

Is Your Software GxP Compliant?

Download a checklist designed for clinical managers in data departments to make sure that software meets requirements for FDA and EMA submissions.
Explore Possibilities

Share Your Data Goals with Us

From advanced analytics to platform development and pharma consulting, we craft solutions tailored to your needs.

Talk to our Experts
python
quarto
ai&research