Skip to content

Commit

Permalink
Virtual env docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris7 committed Dec 5, 2023
1 parent b1b74e8 commit 42155f4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
Binary file added docs/img/ignore_bad_imports.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/venv_admin_page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/venv_change_form.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/venv_set_script_venv.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Getting Started
configuration
running_wooey
scripts
venv_setup
wooey_ui
api
api_keys
Expand Down
39 changes: 39 additions & 0 deletions docs/venv_setup.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
23 changes: 20 additions & 3 deletions wooey/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
Expand Down Expand Up @@ -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",
Expand All @@ -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(
Expand All @@ -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)


Expand Down

0 comments on commit 42155f4

Please sign in to comment.