Making independent executable Python scripts for rendering quarto documents using the Jupyter engine | R-Bloggers

Making independent executable Python scripts for rendering quarto documents using the Jupyter engine | R-Bloggers

[This article was first published on R | Dr Tom Palmer, and kindly contributed to R-bloggers]. (You can report problems here about the content on this page)


Do you want to share your content on R-bloggers? Click here if you have a blog, or here If you don’t.

Introduction

In earlier reports I have treated it
Effective multi-engine quarto documents And also how to use
UV Virtual environments for the NBstata Jupyter -Kernel Perform quarto documents with Stata code. That is why we can use trivial extension uv To manage the virutal environments to perform quarto documents using the jupyter: python3 engine.

The slight inconvenience about this approach When you work on a collaboration project with new Python users, is that you ultimately leave a readme or shell script with the required assignments to activate the environment, install the NBSTATA -Kernel and perform quarto. I feel that this management of the virutal environment is a painful point for new Python users – who probably wonder what the hell is a virtual environment. So I have been looking for a way to simplify this process for them.

A recent
Posted by Matt Dray About the use of UV to carry out independent Python scripts, made me think. Could I produce a similarly independent Python script to perform the rendering for quarto documents using the Jupyter engine. Then my colleagues should only mention the script as an enforceable file on the assignment rule. This would take the trouble to manage the virtual environment.

The independent executable Python script

I came up with the next Python script.

#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.9"
# dependencies = [
#     "jupyterlab>=4.4.3",
#     "jupyterlab-stata-highlight2>=0.1.2",
#     "nbstata>=0.8.3",
# ]
# ///
import subprocess

cmd0 = "python -m nbstata.install --sys-prefix"
retval0 = subprocess.call(cmd0, shell=True)
print('returned value:', retval0)

cmd1  = "quarto render --profile stata-questions"
retval1 = subprocess.call(cmd1, shell=True)
print('returned value:', retval1)

cmd2  = "quarto render --profile stata-solutions"
retval2 = subprocess.call(cmd2, shell=True)
print('returned value:', retval2)
  • The first line, the shebang ensures that it is run by uv run

  • The metadata that defines the Python environment is declared between the

    # /// script
    # ...
    # ///
    
    • If you do not use Stata, say you are the jupyter: python3 Engine then you can jupyterlab-stata-highlight2 And nbstata Submissions and the first group of 3 rules for cmd0.
    • If you use extra Python packages in your code, add them to the list.
  • Then comes the actual code. These are just system calls using the sub -process Module. You can change the number of calls and the calls yourself in the stringcitates as required. I recreated some of the rendering assignments in my recent
    Post about the use of quarto profiles for self -study documents. It is worth pointing out that I have not used the Python Quarto package here, because it is currently a bit too limited for my use (I am not sure if it can display profiles).

Save the script in a file, say causeThen make it feasible with

chmod +x render

Then my colleagues have to do it to implement it

./render

Of course UV and quarto must be installed and on them PATHAnd Stata must be installed locally when you use it. For my colleagues who use Windows, they have to do this from a Git Bash Shell instead of Powershell or CMD Shell (for the Shebang line to work).

If you only use the Quarto Knitr -Engine, you do not need this script because you do not need Jupyter.

And for more information about UV Python scripts, the full documentation is
here.

Summary

I have shown how you can make a self-feasible Python script to display quarto documents using the Jupyter engine that automatically manage their own virtual environment. This means that users do not have to manage the virtual environment themselves, which can be a pain for new Python users.


#Making #independent #executable #Python #scripts #rendering #quarto #documents #Jupyter #engine #RBloggers

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *