Hello @Justinas365,
Thanks for the question.
My suggestion is for you to create a Python package to host main.py and install the Python package.
From a fresh Conda environment:
$ conda create -y -n example python=3.10
$ conda activate example
And the file structure:
.
├── _quarto.yml
├── rep
│ ├── __init__.py
│ └── main.py
├── setup.py
├── SourceReports.qmd
└── SourceReports.Rproj
You can install the package using
$ python -m pip install -e .
Note the -e. It will install the package in "edit" mode. This means that changes to main.py will take effect immediately when you load the package again.
Now, you can run quarto to render your SourceReports.qmd that uses functions from main.py. If you are rendering from RStudio, you need to change the path of the Python interpreter: Tools > Project Options > Python.
Content of the files:
setup.py:
from setuptools import setup
setup(
name='rep',
version='0.1.0',
author='An Awesome Coder',
author_email='aac@example.com',
packages=['rep'],
scripts=[],
url='http://pypi.python.org/pypi/PackageName/',
license='',
description='An awesome package that does something',
long_description='',
install_requires=[
],
)
rep/main.py:
def hello():
return "hello"
_quarto.yml:
project:
title: "SourceReports"
editor: visual
cat SourceReports.qmd:
---
title: "SourceReports"
---
## Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.
```{r}
1 + 1
```
## Python
```{python}
import rep.main
rep.main.hello()
```
SourceReports.Rproj:
Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
PythonType: conda
PythonVersion: 3.10.5
PythonPath: ~/mambaforge/envs/example/bin/python