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

Lazy-install altair and tenacity #9

Draft
wants to merge 4 commits into
base: stlite-1.32.2
Choose a base branch
from
Draft
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 lib/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# - Always include the lower bound as >= VERSION, to keep testing min versions easy
# - And include an upper bound that's < NEXT_MAJOR_VERSION
INSTALL_REQUIRES = [
"altair>=4.0, <6",
# "altair>=4.0, <6", # HACK: For stlite, comment out as it will be lazy-installed when needed. See script_runner.py
"blinker>=1.0.0, <2",
"cachetools>=4.0, <6",
# "click>=7.0, <9", # HACK: For stlite, comment out as it's not needed for stlite
Expand All @@ -49,7 +49,7 @@
# "pyarrow>=7.0", # HACK: For stlite, comment out as it's not Pyodide-compatible
# "requests>=2.27, <3", # HACK: For stlite, comment out as it's not Pyodide-compatible
# "rich>=10.14.0, <14", # HACK: For stlite, comment out as rich is not really needed for stlite
"tenacity>=8.1.0, <9",
# "tenacity>=8.1.0, <9", # HACK: For stlite, comment out as it will be lazy-installed when needed. See script_runner.py
# "toml>=0.10.1, <2", # HACK: For stlite, comment out as it's not needed for stlite
"typing-extensions>=4.3.0, <5",
# Don't require watchdog on MacOS, since it'll fail without xcode tools.
Expand Down
42 changes: 42 additions & 0 deletions lib/streamlit/runtime/scriptrunner/script_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ class ScriptRunnerEvent(Enum):
ENQUEUE_FORWARD_MSG = "ENQUEUE_FORWARD_MSG"


LAZY_INSTALL_LIST = { # map of importable module name to PyPI package name
# Now only supports the packages that some of Streamlit core features depend on but excluded from its dependency list for faster installation.
# PIL (pillow) is not supported now. See the note in the lazy-install block in the _run_script method.
"altair": "altair",
"tenacity": "tenacity",
}
lazy_install_tried_modules = (
set()
) # Holds the module names we've tried to lazy-install to avoid trying again and resulting in an infinite loop.


"""
Note [Threading]
There are two kinds of threads in Streamlit, the main thread and script threads.
Expand Down Expand Up @@ -561,6 +572,37 @@ async def _run_script(self, rerun_data: RerunData) -> None:
premature_stop = True

except Exception as ex:
if isinstance(ex, ImportError):
# Stlite: Lazy-install
# NOTE: This lazy-install mechanism doesn't catch the import errors of
# Pyodide-distributed packages such as PIL (pillow) because ModuleNotFoundError
# will be raised when trying to import instead of ImportError.
# Moreover, that ModuleNotFoundError object won't have the `name` attribute,
# so we can't get the missed module name from it in a straightforward way.
# Then, for now, we gave up supporting it as the concered package is only PIL
# and unshipping and lazy-installing it doesn't introduce so much benefit.
_LOGGER.debug("ImportError: %s", ex)
missed_module_name = ex.name
if (
missed_module_name not in lazy_install_tried_modules
and missed_module_name in LAZY_INSTALL_LIST
):
package_name = LAZY_INSTALL_LIST[missed_module_name]
lazy_install_tried_modules.add(missed_module_name)
_LOGGER.info(
"Attempting to install missing module: %s", package_name
)
try:
import micropip

await micropip.install(package_name)
continue
except Exception as micropip_err:
_LOGGER.warning(
"Failed to lazy-install missing module: %s",
micropip_err,
)

self._session_state[SCRIPT_RUN_WITHOUT_ERRORS_KEY] = False
uncaught_exception = ex
handle_uncaught_app_exception(uncaught_exception)
Expand Down