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

Add a serve target #210

Open
1 task
jafaircl opened this issue May 16, 2024 · 2 comments
Open
1 task

Add a serve target #210

jafaircl opened this issue May 16, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@jafaircl
Copy link

jafaircl commented May 16, 2024

  • I'd be willing to implement this feature

(I would definitely be willing to help implement this feature. But, there are a couple of things I'm not sure how to accomplish)

Description

For local development, it would be very helpful to have a "watch" mode. Other plugins generally name this target serve.

Motivation

Being able to run a script, such as a REST API, with a single command from the workspace root and have it re-run when file contents change would speed up development in workspaces using this plugin.

Suggested Implementation

I'm able to manually add a serve target that uses nodemon to the generated project.json like so:

"serve": {
    "executor": "@nxlv/python:run-commands",
    "options": {
        "command": "npx nodemon -e py --exec \"poetry run python my_project/__init__.py\"",
        "cwd": "my-project"
    }
}

I also modified __init__.py to look like this:

"""Automatically generated by Nx."""

from hello import hello

if __name__ == "__main__":
    print(hello())

But, I'm not sure that would also watch library code. I'm also not sure if nodemon is the right tool for this job or not. Since it's able to run with npx, it would prevent users from having to install additional tooling.

Alternate Implementations

A "more correct" approach would probably be to use watchmedo or something similar. nodemon has worked in my testing. But, that may not be true for everyone.

@jafaircl jafaircl added enhancement New feature or request needs-triage This issue needs triage labels May 16, 2024
@lucasvieirasilva
Copy link
Owner

Hey @jafaircl, thanks for raising this request, I believe if you are using FastAPI you can use the fastapi dev CLI or this tool https://www.uvicorn.org/,

By using the @nxlv/python:run-commands you can achieve that, however, it would be nice to have an executor dedicated to this, the same as Nx does for many platforms, like next.js, react, angular and etc.

I think it's definitely worth the investigation, in the meantime could you explore a little bit on your side if uvicorn or fastapi CLI works for your needs, please let me know the findings so we can implement something based on a real scenario.

@lucasvieirasilva
Copy link
Owner

@jafaircl I've created an example repo that uses the FastAPI CLI to serve the Rest API with watch mode, it worked pretty well, I don't think we need a dedicated Nx executor for that, the serve target is pretty simple and straightforward

https://github.com/lucasvieirasilva/nx-python-fastapi

@lucasvieirasilva lucasvieirasilva removed the needs-triage This issue needs triage label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants