An open API service indexing awesome lists of open source software.

https://github.com/zimmermanncode/nodely

putMORE Node.js into Python
https://github.com/zimmermanncode/nodely

node node-modules nodejs npm npmjs python python-environment python-setup

Last synced: 9 months ago
JSON representation

putMORE Node.js into Python

Awesome Lists containing this project

README

          

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# nodely >>> putMORE Node.js into Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![](http://www.gnu.org/graphics/lgplv3-88x31.png)](\n",
" https://gnu.org/licenses/lgpl.html)\n",
"[![](https://img.shields.io/pypi/pyversions/nodely.svg)](\n",
" https://python.org)\n",
"[![](https://img.shields.io/pypi/v/nodely.svg)](\n",
" https://pypi.python.org/pypi/nodely)\n",
"[![](https://img.shields.io/pypi/dd/nodely.svg)](\n",
" https://pypi.python.org/pypi/nodely)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![](https://travis-ci.org/zimmermanncode/nodely.svg)](\n",
" https://travis-ci.org/zimmermanncode/nodely)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* [**Embed**](#Embed-node_modules/-in-Python-environments)\n",
" **`node_modules/` in Python environments**\n",
"* [**`require`**](#require_node_modules-in-setup.py)**`_node_modules`\n",
" in setup.py** \n",
"* [**Run**](#Run-installed-Node.js-tools-from-Python)\n",
" **installed Node.js tools from Python**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use [pip](http://pip-installer.org) to install the latest [release](\n",
" https://pypi.python.org/pypi/nodely) from [PyPI](https://pypi.python.org):\n",
"\n",
"> `pip install nodely`\n",
"\n",
"And don't forget to install [Node.js](https://nodejs.org) ;)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Embed `node_modules/` in Python environments"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import nodely"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Many great tools are written with JavaScript in [Node.js](https://nodejs.org).\n",
"It makes sense to use them in Python instead of reinventing the wheel.\n",
"`nodely` provides an API for managing local `node_modules/` in Python environments\n",
"and running the installed Node.js tools from Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the root directory of the current Python environment is:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'C:\\\\Users\\\\Zimmermann\\\\Miniconda3\\\\envs\\\\nodely'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sys\n",
"\n",
"sys.prefix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then `nodely` will create:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Path('C:\\\\Users\\\\Zimmermann\\\\Miniconda3\\\\envs\\\\nodely\\\\node_modules')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nodely.NODE_MODULES_DIR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"_Please don't modify the above constant, except you exactly know what you are doing ;)_"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's say you want to use the [CoffeeScript](http://coffeescript.org) compiler...\n",
"Just install the Node.js package:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"nodely.install('coffee-script')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It provides the `coffee` executable. If you want to know its absolute path:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Path('C:\\\\Users\\\\Zimmermann\\\\Miniconda3\\\\envs\\\\nodely\\\\node_modules\\\\.bin\\\\coffee.CMD')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nodely.which('coffee')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And if you want to run it, for example with the `--version` flag:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nodely.call('coffee', ['--version'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the case that you want to get rid of the package again,\n",
"just `nodely.uninstall('coffee-script')` it"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `require_node_modules` in setup.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instead of installing Node.js packages during runtime,\n",
"you can also define them as dependencies of your Python package:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"from setuptools import setup\n",
"\n",
"setup(\n",
" ...\n",
" setup_requires=['nodely', ...],\n",
" require_node_modules=['coffee-script', ...],\n",
" ...\n",
")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So they get implicitly installed during the installation of the Python package,\n",
"just like the Python dependencies defined in `install_requires`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run installed Node.js tools from Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `nodely.call` function additionally supports `subprocess.call` options:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from subprocess import DEVNULL\n",
"\n",
"nodely.call('coffee', ['--version'], stdout=DEVNULL)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And instead of a simple `nodely.call`,\n",
"you can also create a process instance,\n",
"and give any `subprocess.Popen` options to it:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from subprocess import PIPE\n",
"\n",
"process = nodely.Popen('coffee', ['--version'], stdout=PIPE,\n",
" universal_newlines=True)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.12.7'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"process.communicate()[0].split()[-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A more object-oriented approach is provided by:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import nodely.bin"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It lets you introspect all installed executables with interactive auto-completion\n",
"and creates `nodely.bin.Command` instances:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"coffee = nodely.bin.coffee"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`nodely.bin['coffee']` returns the same.\n",
"And that `nodely.bin.Command` instance provides its own `.call` and a `.Popen` methods,\n",
"and can also be called directly instead of using its `.call` method:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"coffee(['--version'])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}