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

Support for Automatic <py-env> with dependency + few tweaks to contributing and readme docs #36

Open
wants to merge 36 commits into
base: main
Choose a base branch
from

Conversation

leriomaggio
Copy link

This PR finalises the new pyscript-cli features developed during the last hack-days.

In particular, the new features this PR brings are:

  • support for automatic analysis of code dependencies (i.e. imports) which will be then injected into a <py-env> tag in the html template.
    • both packages and (local) modules are supported - resulting in paths as per PyScript naming.
    • Unsupported imports and packages (e.g. packages not supported by Pyodide) are also detected, with a WARNING raised to signal that resulting code may not work.
  • now both Python files (.py) and Notebooks (.ipynb) could be passed in as input and converted.
    • so far notebooks are merely converted into Python files before being processed.

Note: from hack-days version, this code has been merged and aligned to support the new plugin-hook design/feature, plus - more importantly - a few tests for the new features have been included.
On this note, a quick refactoring has been done to test_cli to include fixtures for code, with relatively simple injectable dependencies.

Other changes in this PR concerns:

  • improved CONTRIBUTING.md documentation providing instructions on how to setup dev environment (not just for docs)
    • on this note, poetry dependencies have been revised and updated, including pre-commit as dependency, with instructions in the main doc on how to install hooks.
  • added line in README.md to showcase input code from both python and notebooks files.

The new module extends the ModuleFinder from find-imports
(in graveyard) to detect all the dependencies of the
file being converted.

Currently the parser is able to collect either
external packages (limited to those supported by Pyodide)
and any local module to be included in `paths`.

Local submodules (node.level > 0) not yet supported.
Code parsing has been completely refactored and improved
to support the identification of corner import conditions
that won't work in Pyscript.
This includes unsupported packages, and local modules/packages
import that won't work if not imported directly.

As a result, ModuleFinder gathers all the packages via NamespaceInfo
(populated via pkgutil) and collects results in a FinderResult
object.
This object will be later used to specialise the processing result.
find_imports functions now leverages directly
on the FinderResults class to encapsualte
parsing results.
Warning message is now generated (in yellow) when the converted
Python module contains unsupported packages or local modules
(as in packages)
nbconvert is an extra dependency required to support the conversion of notebooks into python files.

Pre-commit on the other hand was a potentially missing dependency for dev.

Last but not least, requests and six are also needed to be included otherwise those will be removed at every `poetry install`
Also added missing dependencies
Also added missing dependencies
and removed useless code from prev. manual merge
this commit includes a few general abstractions to ease
the testing of cli with multiple py-code snippets.

In particular, the default py-code (no imports) has been wrapped into fixture, and all tests changed accordingly to avoid hard-coding py-code.

Moreover, a new py_code_with_import fixture has been added to support testing of the new py-env integration feature.

A new test has been added, having multiple dependencies dynamically injected via `Dependency` dataclass.
ipykernel dependency is required by nbconvert to convert
notebooks into Python code files.
@mattkram
Copy link
Collaborator

mattkram commented Sep 7, 2022

@leriomaggio Sorry, I have forgotten to configure isort to prevent conflicts with black during pre-commit. Can you please add the following to pyproject.toml?

[tool.isort]
profile = "black"

@leriomaggio
Copy link
Author

@leriomaggio Sorry, I have forgotten to configure isort to prevent conflicts with black during pre-commit. Can you please add the following to pyproject.toml?

[tool.isort]
profile = "black"

YES! that was giving me headaches already!! will do :D

@leriomaggio
Copy link
Author

leriomaggio commented Sep 7, 2022

@mattkram Any idea on why the tests are failing ? - quickly looking on the internet it seems an issue with poetry version? but not sure.. REF

@leriomaggio
Copy link
Author

@mattkram Any idea on why the tests are failing ? - quickly looking on the internet it seems an issue with poetry version? but not sure.. REF

Tried to update poetry-core dep to see if that fixes the issue

(as per instructions from poetry#4983

@mattkram
Copy link
Collaborator

mattkram commented Sep 7, 2022

@mattkram Any idea on why the tests are failing ? - quickly looking on the internet it seems an issue with poetry version? but not sure.. REF

Tried to update poetry-core dep to see if that fixes the issue

(as per instructions from poetry#4983

I'm not sure but haven't had a chance to really look yet. I believe they just released poetry v2 so it's quite possible there were some API changes. If we are doing a blind pip install poetry without specifying the max version we may be getting the wrong one.

I will take a look later today when I get a chance.

@leriomaggio
Copy link
Author

leriomaggio commented Sep 7, 2022

@mattkram Any idea on why the tests are failing ? - quickly looking on the internet it seems an issue with poetry version? but not sure.. REF

Tried to update poetry-core dep to see if that fixes the issue
(as per instructions from poetry#4983

I'm not sure but haven't had a chance to really look yet. I believe they just released poetry v2 so it's quite possible there were some API changes. If we are doing a blind pip install poetry without specifying the max version we may be getting the wrong one.

I will take a look later today when I get a chance.

So what I did was essentially changing (manually) the requirement for poetry-core >=1.1.0b3. The last comment in the issue is indeed dated on 15th of July so the problem (and workaround) seems to be there still.

However, looking at setup in github actions, poetry 1.1.6 seems to be installed, instead of poetry 1.2 - not sure how I could control this? 🤔

@codecov-commenter
Copy link

codecov-commenter commented Sep 7, 2022

Codecov Report

Merging #36 (29bfdda) into main (4477ce6) will decrease coverage by 2.72%.
The diff coverage is 91.08%.

@@            Coverage Diff             @@
##             main      #36      +/-   ##
==========================================
- Coverage   96.78%   94.06%   -2.73%     
==========================================
  Files          10       12       +2     
  Lines         249      438     +189     
==========================================
+ Hits          241      412     +171     
- Misses          8       26      +18     
Impacted Files Coverage Δ
src/pyscript/_generator.py 82.92% <70.83%> (-17.08%) ⬇️
src/pyscript/plugins/wrap.py 97.82% <88.88%> (-2.18%) ⬇️
src/pyscript/_node_parser.py 90.19% <90.19%> (ø)
src/pyscript/_supported_packages.py 100.00% <100.00%> (ø)
src/pyscript/cli.py 88.57% <100.00%> (+1.47%) ⬆️
tests/test_cli.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mattkram
Copy link
Collaborator

mattkram commented Sep 7, 2022

@leriomaggio I believe the poetry issue was that there was an added section in pyproject.toml that was invalid. I commented it out, but we should remove it before we merge. Just wanna check that they are valid actual dependencies, or if they should be moved to the dev dependencies section.

I also cleaned up some type hints.

I will give the rest of the code a more thorough review later, but I wanted to get the CI passing for you. Gotta hone this automation (or throw it away if it is too onerous for contributors).

@leriomaggio
Copy link
Author

@mattkram thanks very much for taking the time to have a look at that - and for making the automation to pass.

Re poetry and group: I thought the idea was to keep deps separated from installation (from source) and dev/contribution.
In fact, in the new section "how to setup the env" I added in CONTRIBUTING.md, I mentioned the two options.

I think the question is whether we want to have these groups, or to keep it simple? Or maybe move that one to extra too?

So far the dev group would merely include pre-commit, to setup hooks that would help prepping the code for PR and adhere to coding conventions, and pytest. On this note, thanks for fixing the typing annotation I missed! 🙌 Appreciated!

As for the other deps in poetry: I think I double checked those, so they should all be required by pyscript-cli to run - including the new nbconvert one for notebook conversion. But please let me know, in case :)

@ntoll
Copy link
Member

ntoll commented Sep 8, 2022

FWIW, in the plugins we're developing (with poetry) we're using the new groups based dependency schema as per poetry 1.2.

Copy link
Member

@ntoll ntoll left a comment

Choose a reason for hiding this comment

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

The code looks great. Nice job. I honestly don't have anything to add to nor change about it. 👍

Now comes the interesting bit, the way we specify dependencies is in a state of flux and what we have in this PR is going to be out of date very soon.

  • In projects (made by the create command) dependencies will be listed in a manifest.toml file.
  • The HTML pages inside the project will reference this manifest.toml file, and may additionally have a py-env tag containing additional dependencies or package overrides. This inner-text of this tag will be TOML (not YAML as it is now).
  • For non-project, single page, PyScript work (as created by the wrap command) the py-env will simply contain a TOML specification of the dependencies.

While I've approved this PR, FIRST we need to decide (all input welcome!):

  • Merge this (soon to be changed) work now?
    or
  • Wait until the config/env/toml work is settled down and merged into PyScript core before merging this work (modified to match the new schema).

@leriomaggio
Copy link
Author

Thanks @ntoll for the review and for finding the time to go though the code! 😊

Now comes the interesting bit,
[...]
This is all very very interesting! I am very much looking forward to knowing more about this!

While I've approved this PR, FIRST we need to decide (all input welcome!): [...]

This is indeed a very good point! Just throwing a couple of points here that could hopefully give food for thoughts to help deciding about this (or not?! 😅 )

  1. If I understood correctly, what's going to be soon deprecated is how the py-env environment is defined. Which means, for the sake of this PR, changing how the dependencies will finally end up in the template. (pls correct me if I am wrong!). So, from my perspective, this would mean fixing only half of it... :)

  2. I am working on my upcoming Webinar on PyScript for Nucleus (due on the 21st) and I was hoping to mention pyscript-cli and have it showcased as well! I will specifically mention py-env as a key PyScript's feature for Data Science, so having cli with py-env support would've been super great (from my very biased pov)

HTH

@FabioRosado
Copy link
Contributor

@leriomaggio Seems like this PR was forgotten for a long time 😬
I'm going to try and fix the conflicts and make the necessary changes to get this in 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants