Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/codestyle-python-flake8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
run: pip install -r dev-requirements.txt
- name: Install Flake8
run: pip install flake8
- name: Run Flake8
run: flake8 python tests/python tests/fixtures
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.1.2
49 changes: 25 additions & 24 deletions python/vim_mlflow.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import contextlib
import io
import json
import math
import os
from urllib.parse import urlencode
from urllib.request import urlopen

import mlflow
import vim
from mlflow.entities import ViewType
from mlflow.tracking import MlflowClient

from vim_mlflow_cache import get_session

Expand Down Expand Up @@ -316,27 +313,29 @@ def _downsample_points(points, target_len):
return downsampled


def _collect_artifacts(client, run_id, path="", depth=0, max_depth=50):
def _collect_artifacts(tracking_uri, run_id, path="", depth=0, max_depth=50):
"""Collect an artifact tree for one run up to the requested depth."""
nodes = []
try:
actual_path = path or None
if actual_path is None:
artifacts = client.list_artifacts(run_id)
else:
artifacts = client.list_artifacts(run_id, actual_path)
params = {"run_id": run_id}
if path:
params["path"] = path
url = f"{tracking_uri.rstrip('/')}/api/2.0/mlflow/artifacts/list?{urlencode(params)}"
with urlopen(url) as response:
data = json.loads(response.read())
artifacts = data.get("files", [])
except Exception:
return nodes
for item in sorted(artifacts, key=lambda a: a.path):
for item in sorted(artifacts, key=lambda a: a["path"]):
node = {
"path": item.path,
"name": item.path.rsplit("/", 1)[-1],
"is_dir": item.is_dir,
"path": item["path"],
"name": item["path"].rsplit("/", 1)[-1],
"is_dir": item.get("is_dir", False),
"children": [],
}
if item.is_dir and depth < max_depth:
if item.get("is_dir") and depth < max_depth:
node["children"] = _collect_artifacts(
client, run_id, item.path, depth + 1, max_depth
tracking_uri, run_id, item["path"], depth + 1, max_depth
)
nodes.append(node)
return nodes
Expand Down Expand Up @@ -411,15 +410,17 @@ def walk(nodes, depth):

def download_artifact_file(tracking_uri, run_id, artifact_path, target_dir):
"""Download one artifact file for local viewing."""
mlflow.set_tracking_uri(tracking_uri)
client = MlflowClient()
if not tracking_uri.startswith(("http://", "https://")):
raise ValueError(f"tracking_uri must be http:// or https://, got: {tracking_uri!r}")
os.makedirs(target_dir, exist_ok=True)
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(
io.StringIO()
):
local_path = client.download_artifacts(
run_id, artifact_path, dst_path=target_dir
)
params = urlencode({"run_uuid": run_id, "path": artifact_path})
url = f"{tracking_uri.rstrip('/')}/get-artifact?{params}"
local_path = os.path.join(target_dir, run_id, *artifact_path.split("/"))
os.makedirs(os.path.dirname(local_path), exist_ok=True)
with urlopen(url) as response:
content = response.read()
with open(local_path, "wb") as f:
f.write(content)
if os.path.isdir(local_path):
raise IsADirectoryError(f"{artifact_path} is a directory")
return local_path
Expand Down
2 changes: 1 addition & 1 deletion python/vim_mlflow_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def get_artifact_tree(
self.artifacts_by_run.pop(key, None)
if key not in self.artifacts_by_run:
self.artifacts_by_run[key] = collector(
self.client, str(run_id), max_depth=int(max_depth)
self.tracking_uri, str(run_id), max_depth=int(max_depth)
)
return self.artifacts_by_run[key]

Expand Down
Loading