posit-dev / py-shinylive

Python package for deploying Shinylive applications
https://shiny.posit.co/py/docs/shinylive.html
MIT License
44 stars 5 forks source link

If a `requirements.txt` is found, only inspect those packages for dependencies #17

Open schloerke opened 12 months ago

schloerke commented 12 months ago

Request:

Hmm, shinylive appears to scan every .py file in the directory to figure out what pyodide needs to install, even scripts that aren't used at runtime. Like sometimes I'll have a helper script that builds a database (in this case, using sqlalchemy) but the actual shiny app only needs built-in sqlite3.

I hacked around this by making a stripped-down branch and exporting that, but it could be nice if either the build process honored requirements.txt (in a minimalistic sense) or if somehow it only scanned app.py and what it imports? or some other solution to ignore certain files when figuring this out.

My reply:

... correct. py-shinylive currently scans every that app.json provides. (Which is every file.) Link

It seems fair to me that if a requirements.txt is found, then only those packages are inspected.

coatless commented 6 months ago

For anyone else who stumbles upon this, the easiest way to sidestep having shinylive attempt to load packages in a requirements.txt is to rename the file to either requirements-dev.txt or another name. You can see an example app using requirements-dev.txt working.

Screenshot showing an example of a shinylive app working by renaming the `requirements.txt` file to `requirements-dev.txt`

Alternatively, feel free to check out a demo app using requirements.txt instead of requirements-dev.txt failing if just shinylive is present due its dependency on the lzstring package, which lacks a Pyodide-compiled Python package wheel available.

ValueError: Can't find a pure Python 3 wheel for 'lzstring>=1.0.4'. ```py Traceback (most recent call last): File "", line 368, in _start_app File "", line 302, in _install_requirements_from_dir File "/lib/python3.10/site-packages/micropip/_micropip.py", line 573, in install await transaction.gather_requirements(requirements) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 333, in gather_requirements await gather(*requirement_promises) File "/lib/python3.10/asyncio/futures.py", line 284, in __await__ yield self # This tells Task to wait for completion. File "/lib/python3.10/asyncio/tasks.py", line 304, in __wakeup future.result() File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 234, in __step result = coro.throw(exc) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 340, in add_requirement return await self.add_requirement_inner(Requirement(req)) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 448, in add_requirement_inner await self.add_wheel(wheel, req.extras) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 463, in add_wheel await self.gather_requirements(wheel.requires(extras)) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 333, in gather_requirements await gather(*requirement_promises) File "/lib/python3.10/asyncio/futures.py", line 284, in __await__ yield self # This tells Task to wait for completion. File "/lib/python3.10/asyncio/tasks.py", line 304, in __wakeup future.result() File "/lib/python3.10/asyncio/futures.py", line 201, in result raise self._exception File "/lib/python3.10/asyncio/tasks.py", line 232, in __step result = coro.send(None) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 337, in add_requirement return await self.add_requirement_inner(req) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 435, in add_requirement_inner wheel = find_wheel(metadata, req) File "/lib/python3.10/site-packages/micropip/_micropip.py", line 303, in find_wheel raise ValueError( ValueError: Can't find a pure Python 3 wheel for 'lzstring>=1.0.4'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel You can use `micropip.install(..., keep_going=True)`to get a list of all packages with missing wheels. ```
Screenshot showing an example of a shinylive app failing with a `requirements.txt` file that includes the `shinylive` package itself!