Venv docs (#394)

* Virtual env docs

* Fix docs

* add token
This commit is contained in:
Chris Mitchell 2023-12-05 09:28:48 -05:00 committed by GitHub
parent b1b74e8276
commit 088519ba44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 3 deletions

View File

@ -42,6 +42,7 @@ jobs:
- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
flags: unittests
name: codecov-umbrella

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -36,6 +36,7 @@ Getting Started
configuration
running_wooey
scripts
venv_setup
wooey_ui
api
api_keys

41
docs/venv_setup.rst Normal file
View File

@ -0,0 +1,41 @@
Virtual Environment Setup
=========================
Virtual environments allow you to specify a python interpreter and a set of requirements to run a script in.
Like with script addition, virtual environment setup is done via the Django admin.
Adding a virtual environment
----------------------------
Virtual environments may be setup from the admin under the `Virtual Environments` section:
.. image:: img/venv_admin_page.png
To add a virtual environment, click the Add button and a form will be rendered for setting up a new virtual environment.
.. image:: img/venv_change_form.png
The fields are:
* **name** What to call the virtual environment. Virtual environments can be reused across scripts if desired.
* **python binary** The path to a python executable to create and use for running the virtual environment
* **requirements** This is equivalent to the requirements.txt file for defining packages to install
* **Venv directory** Where to store the virtual environment. The default location for this can be defined via the `WOOEY_VIRTUAL_ENVIRONMENT_DIRECTORY` setting. If not defined, this defaults to the system temporary directory folder.
Lastly, to add the virtual environment to a script, from the script admin view, choose the virtual environment to run
the script in:
.. image:: img/venv_set_script_venv.png
Adding scripts with invalid imports
-----------------------------------
Virtual environments are meant to have requirements that may not be present on the main Wooey server. Thus, some
scripts may fail to import because of dependency conflicts. To resolve this, a new option is available on scripts,
`ignore_bad_imports`, that may be set via the admin.
.. image:: img/ignore_bad_imports.png

View File

@ -11,6 +11,7 @@ from threading import Thread
from django.utils.text import get_valid_filename
from django.core.files import File
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from celery import app
from celery.schedules import crontab
@ -143,6 +144,7 @@ def setup_venv(virtual_environment, job=None, stdout="", stderr=""):
return_code = 0
if not os.path.exists(venv_path):
stdout += _("Setting up Virtual Environment\n########\n")
venv_command = [
virtual_environment.python_binary,
"-m",
@ -156,13 +158,22 @@ def setup_venv(virtual_environment, job=None, stdout="", stderr=""):
)
if return_code:
raise Exception("VirtualEnv setup failed.\n{}\n{}".format(stdout, stderr))
raise Exception(
_("VirtualEnv setup failed.\n{stdout}\n{stderr}").format(
stdout=stdout, stderr=stderr
)
)
pip_setup = [venv_executable, "-m", "pip", "install", "-I", "pip"]
stdout += _("Installing Pip\n########\n")
(stdout, stderr, return_code) = run_and_stream_command(
pip_setup, cwd=None, job=job, stdout=stdout, stderr=stderr
)
if return_code:
raise Exception("Pip setup failed.\n{}\n{}".format(stdout, stderr))
raise Exception(
_("Pip setup failed.\n{stdout}\n{stderr}").format(
stdout=stdout, stderr=stderr
)
)
requirements = virtual_environment.requirements
if requirements:
with tempfile.NamedTemporaryFile(
@ -177,12 +188,18 @@ def setup_venv(virtual_environment, job=None, stdout="", stderr=""):
"-r",
reqs_txt.name,
]
stdout += _("Installing Requirements\n########\n")
(stdout, stderr, return_code) = run_and_stream_command(
venv_command, cwd=None, job=job, stdout=stdout, stderr=stderr
)
if return_code:
raise Exception("Requirements setup failed.\n{}\n{}".format(stdout, stderr))
raise Exception(
_("Requirements setup failed.\n{stdout}\n{stderr}").format(
stdout=stdout, stderr=stderr
)
)
os.remove(reqs_txt.name)
stdout += _("Virtual Environment Setup Complete\n########\n")
return (venv_executable, stdout, stderr, return_code)