hogarthww-labs / nx-python

Python plugin for Nx with support for Django and Flask
MIT License
17 stars 2 forks source link

šŸ NxPy: Nx Python plugin

This project was generated using Nx.

The nx-python plugin allows users to create a basic python application using nx commands. To add the plugin to your project, just follow these steps:

Getting started

Create a Nx Workspace

Before installing the nx-python plugin, it is required to have a pre-configured Nx Workspace . If you don't, then proceed to create a new one executing the following command:

npx create-nx-workspace python-workspace --preset=empty --cli=nx --nx-cloud true
cd python-workspace

Install the nx-python plugin

yarn add -D @nx-python/nx-python

Adding an application to our workspace

To create a new python application based on this plugin, please execute the following command:

nx g @nx-python/nx-python:app <app-name>

Note: Replace <app-name> with the name of your new application

Advanced options

Create Flask app

nx g @nx-python/nx-python:app <app-name> --template flask

Create Django app

nx g @nx-python/nx-python:app <app-name> --template django

Advanced configuration

The plugin supports cosmiconfig for global configuration.

Simply add a .nx-pythonrc file to the root of your monorepo. Currently this configuration support specifying command options to be used

{
  "python": "python3",
  "commands": {
    "serve": "flask run",
    "lint": "-m pylint",
    "test": "-m pytest",
  }
}

See src/utils/commands.ts for command examples

You can also specify project specific command overrides in workspace.json project entries or in project.json per project

"my-app": {
  "python": "python3",
  "commands": {
    "serve": "flask run",
    "lint": "-m pylint",
    "test": "-m unittest discover -s ./ -p",
  }
}

Usage

After creating the <app-name> application using the nx-python plugin. We can execute the build, lint, test and serve nx commands on this new application. Output will be stored on dist/<app-name>/ directory.

Building the app

The build command is going to compile all the python files inside <app-name> directory, using the py_compile native module.

nx build <app-name>

More information here: py_compile

Linting the application

Unfortunately Python doesn't have a native linting module(yet!). It is required that you install the linting module beforehand.

After you have installed the linting module you can perform the lint process with:

nx lint <app-name>

Flake8

nx-python uses the Flake8 module by default to lint your application.

More info here: Flake8

pip install Flake8

Serving the application

This is going to execute the main file in your python application.

nx serve <app-name>

For flask and django, the main file is manage.py

Testing your application

The test command is going to execute all the test units inside your python app. You can add new test unit files.

To test your python app, execute the command:

nx test <app-name>

Unittest

If you are using the default native unittest python module to for tests:

See more info here: unittest

Dependency Graph

packages/project-graph includes some code which detects python projects and adds modified files in python projects to the dependency graph for the project.

To detect a python project it must either:

Contributing

šŸ All contributions are welcome. Make sure you follow the code of conduct in this repository.

MIT License

Made with šŸ’œ by Code ON | Melvin Vega & Diana Rodriguez