-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Command line tool to extract code #389
Comments
Note, currently you could do: quarto convert test.qmd
ipython nbconvert --to python test.ipynb A reasonable work-around for now, although I wonder how it deals with qmd files containing more than one language. |
Implementing a Using Note that Jupytext (https://jupytext.readthedocs.io/en/latest/) also supports Quarto (it calls |
Is there anything working for files that only use R code? Ideally:
I think this could make it a lot easier/better for Quarto people to work well with non-Quarto-ists. (It also could be useful for Quarto-lovers like myself in some situations). |
By the way, the following command in the post by @Robinlovelace :
does not work with jupyter nbconvert --to python test.ipynb |
Duplicate of #588 |
Just to make it easier, I've made a simple script for this: #!/bin/bash
# Script: convert_qmd_to_py.sh
#
# Description: Convert .qmd files to Python scripts and optionally execute them.
#
# Usage: convert_qmd_to_py.sh [-x|--execute] [-o|--output OUTPUT_DIR] FILES...
#
# Options:
# -x, --execute Execute the converted Python script after conversion.
# -o, --output Specify the output directory for the converted Python files.
# -h, --help Display this help message and exit.
#
# Arguments:
# FILES One or more .qmd files to convert.
# Function to display help
show_help() {
cat <<EOF
Usage: convert_qmd_to_py.sh [-x|--execute] [-o|--output OUTPUT_DIR] FILES...
Convert .qmd files to Python scripts and optionally execute them.
Options:
-x, --execute Execute the converted Python script after conversion.
-o, --output Specify the output directory for the converted Python files.
-h, --help Display this help message and exit.
Arguments:
FILES One or more .qmd files to convert.
EOF
}
# Initialize variables
execute_script=false
quiet=false
output_dir="."
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
-x | --execute)
execute_script=true
shift
;;
-o | --output)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
output_dir="$2"
shift 2
else
echo "Error: No output directory specified for -o option."
exit 1
fi
;;
-q | --quiet)
quiet=true
shift
;;
-h | --help)
show_help
exit 0
;;
*)
files+=("$1")
shift
;;
esac
done
# Check if files are provided
if [[ "${#files[@]}" -eq 0 ]]; then
echo "Error: No .qmd files provided."
show_help
exit 1
fi
# Ensure output directory exists
mkdir -p "$output_dir"
# Process each .qmd file
for file in "${files[@]}"; do
if [[ ! -f "$file" ]]; then
echo "Error: File '$file' does not exist."
continue
fi
# Extract base name without extension
base_name=$(basename "$file" .qmd)
# Convert .qmd to .ipynb
if [[ "$quiet" == false ]]; then
quarto convert "$file" --output "$base_name.ipynb"
else
quarto convert "$file" --output "$base_name.ipynb" 1>/dev/null 2>&1
fi
# Convert .ipynb to .py
if [[ "$quiet" == false ]]; then
jupyter nbconvert --to python "$base_name.ipynb" --output "$base_name.py"
else
jupyter nbconvert --to python "$base_name.ipynb" --output "$base_name.py" 1>/dev/null 2>&1
fi
# Remove intermediate .ipynb file
rm "$base_name.ipynb"
# Optionally execute the Python script
if [[ "$execute_script" == true ]]; then
python "$base_name.py"
rm "$base_name.py"
fi
done |
Awesome! Would be even better if it were functionality built into the Quarto CLI. |
Thanks for the links, loads of rich content in there, e.g. the first code chunk in the last link is as follows:
I wonder if the same job could be achieved with a shorter incantation at some point e.g. quarto extract --language=Python index.qmd |
@Robinlovelace FWIW We plan to provide such feature in packages for now, like the R package and possibly its python counterpart. |
Worth a lot! I like the modular approach, putting it in packages. |
The knitr function
purl()
is really handy for extracting code chunks from RMarkdown files.It would be really useful to have such functionality in Quarto. I've used
quarto convert
to great effect. Could there be command line functionality likeOr perhaps a separate tool, e.g.
The text was updated successfully, but these errors were encountered: