Skip to content

For Express, emit message if need to upgrade rsconnect-python #1233

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

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Changes from 1 commit
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
49 changes: 48 additions & 1 deletion shiny/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import sys
import types
from pathlib import Path
from typing import Any, Optional
from typing import Any, Literal, Optional

import click
import uvicorn
Expand Down Expand Up @@ -288,6 +288,9 @@ def run_app(
# is "shiny.express.app:_2f_path_2f_to_2f_app_2e_py".
app = "shiny.express.app:" + escape_to_var_name(str(app_path))
app_dir = str(app_path.parent)

# Express apps need min version of rsconnect-python to deploy correctly.
_verify_rsconnect_version()
else:
app, app_dir = resolve_app(app, app_dir)

Expand Down Expand Up @@ -626,3 +629,47 @@ class ReloadArgs(TypedDict):
reload_includes: NotRequired[list[str]]
reload_excludes: NotRequired[list[str]]
reload_dirs: NotRequired[list[str]]


# Check that the version of rsconnect supports Shiny Express; can be removed in the
# future once this version of rsconnect is widely used. (Added 2024-03)
def _verify_rsconnect_version() -> None:
PACKAGE_NAME = "rsconnect-python"
MIN_VERSION = "1.22.0"

from importlib.metadata import PackageNotFoundError, version

def _safe_int(x: str) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already have an indirect dependency on packaging, do we want to make it direct and use their version parsing?

    from packaging.version import parse
    from importlib.metadata import PackageNotFoundError, version
    import sys

    try:
        installed_version = parse(version(PACKAGE_NAME))
        required_version = parse(MIN_VERSION)
        if installed_version < required_version:
      ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call -- I see that htmltools uses packaging to check versions, so I've updated the code as you suggested.

try:
return int(x)
except ValueError:
return 0

def _compare_package_versions(version1: str, version2: str) -> Literal[-1, 0, 1]:
parts1 = [_safe_int(x) for x in version1.split(".")]
parts2 = [_safe_int(x) for x in version2.split(".")]

max_length = max(len(parts1), len(parts2))
parts1 += [0] * (max_length - len(parts1))
parts2 += [0] * (max_length - len(parts2))

for part1, part2 in zip(parts1, parts2):
if part1 > part2:
return 1
elif part1 < part2:
return -1

return 0

try:
package_version = version(PACKAGE_NAME)
print(package_version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errant print?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops, I had deleted it on my local copy of the file, but forgot to commit. 🤦

if _compare_package_versions(package_version, MIN_VERSION) < 0:
print(
f"Warning: rsconnect-python {package_version} is installed, but it does not support deploying Shiny Express applications. "
f"Please upgrade to at least version {MIN_VERSION}. "
"If you are using pip, you can run `pip install --upgrade rsconnect-python`",
file=sys.stderr,
)
except PackageNotFoundError:
pass