Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Fixup python-api docs slightly #2285

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions docs/source/python_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@ Mamba provides high-level Python APIs for creating environments and installing p
# install(env_name, packages, channels)
install('my-custom-env', ('matplotlib=3', 'ipympl'), ('conda-forge', ))

These call internal ``mamba`` APIs under the hood, which are discussed below.

Using internal mamba APIs
-------------------------

The core of ``mamba`` is written in C++, but we expose the internals of mamba with a Python API (using ``pybind11``).

You can import the ``mamba_api`` using ``from mamba import mamba_api``.
You can import ``libmambapy`` containing the Python API using ``import libmambapy`` without having ``mamba`` installed.

The mamba_api exposes the following objects:
These bindings expose the following objects (`boa_` has a full example):

- ``Context``: a singleton configuration object. All global configuration goes through this. From Python you can use the context object like so:

.. code::

from mamba import mamba_api
mamba_api.Context().prefix_params.conda_prefix = "/home/wolfv/conda"
ctx = mamba_api.Context()
print(ctx.prefix_params.root_prefix)
import libmambapy
libmambapy.Context().conda_prefix = "/home/wolfv/conda"
ctx = libmambapy.Context()
print(ctx.root_prefix)


Here is an example usage of the mamba_api:
Here is an example usage of the libmambapy:

.. code::

Expand All @@ -52,7 +54,7 @@ Here is an example usage of the mamba_api:
):
check_allowlist(channel_urls)

dlist = mamba_api.DownloadTargetList()
dlist = libmambapy.DownloadTargetList()

index = []
for idx, url in enumerate(channel_urls):
Expand All @@ -68,12 +70,12 @@ Here is an example usage of the mamba_api:
name_and_subdir = channel.subdir
else:
name_and_subdir = channel.name + "/" + channel.subdir
sd = mamba_api.SubdirData(name_and_subdir, full_url, full_path_cache)
sd = libmambapy.SubdirData(name_and_subdir, full_url, full_path_cache)

index.append((sd, channel))
dlist.add(sd)

is_downloaded = dlist.download(mamba_api.MAMBA_DOWNLOAD_FAILFAST)
is_downloaded = dlist.download(libmambapy.MAMBA_DOWNLOAD_FAILFAST)

if not is_downloaded:
raise RuntimeError("Error downloading repodata.")
Expand All @@ -83,21 +85,21 @@ Here is an example usage of the mamba_api:
class MambaSolver:
def __init__(self, prefix, channels, platform):

api_ctx = mamba_api.Context()
api_ctx.prefix_params.conda_prefix = prefix
api_ctx = libmambapy.Context()
api_ctx.conda_prefix = prefix

self.channels = channels
self.platform = platform
self.index = get_index(channels, platform=platform)
self.local_index = []
self.pool = mamba_api.Pool()
self.pool = libmambapy.Pool()
self.repos = []

start_prio = len(channels)
priority = start_prio
subpriority = 0 # wrong! :)
for subdir, channel in self.index:
repo = mamba_api.Repo(
repo = libmambapy.Repo(
self.pool,
str(channel),
subdir.cache_path(),
Expand All @@ -122,11 +124,11 @@ Here is an example usage of the mamba_api:
solvable : bool
True if the set of specs has a solution, False otherwise.
"""
solver_options = [(mamba_api.SOLVER_FLAG_ALLOW_DOWNGRADE, 1)]
api_solver = mamba_api.Solver(self.pool, solver_options)
solver_options = [(libmambapy.SOLVER_FLAG_ALLOW_DOWNGRADE, 1)]
api_solver = libmambapy.Solver(self.pool, solver_options)
_specs = specs

api_solver.add_jobs(_specs, mamba_api.SOLVER_INSTALL)
api_solver.add_jobs(_specs, libmambapy.SOLVER_INSTALL)
success = api_solver.try_solve()

if not success:
Expand All @@ -142,12 +144,12 @@ Here is an example usage of the mamba_api:
print(error_string)
exit(1)

package_cache = mamba_api.MultiPackageCache(pkgs_dirs)
package_cache = libmambapy.MultiPackageCache(pkgs_dirs)

t = mamba_api.Transaction(api_solver, package_cache)
t = libmambapy.Transaction(api_solver, package_cache)
return t

``mamba.api`` also has a default implementation of ``MambaSolver`` which can be
customized with ``Context`` objects.

Let's walk through this example:

We first use the ``get_index`` method to download repository data from the channels.
.. _boa: https://www.github.com/mamba-org/boa/blob/main/boa/core/solver.py