Skip to content

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

pythonodejs 🐍➕🟢

PyPI version Python versions License: MIT Downloads

Embed Node.js natively in Python with seamless integration.

📋 Overview

pythonodejs lets you harness the power of the Node.js ecosystem directly within your Python applications. Run JavaScript code, use NPM packages, and leverage the best of both worlds in a single application.

🚀 Installation

pip install pythonodejs

🔍 Quick Example

from pythonodejs import node_eval

# Execute JavaScript directly
node_eval("console.log('Hello from Node.js in Python!')")
# > Hello from Node.js in Python!

# Create and return JavaScript functions
read_file = node_eval("""
const fs = require('fs');

function readFile(filePath) {
    return fs.readFileSync(filePath, 'utf8');
}

readFile; // Return the function
""")

# Call JavaScript functions from Python
content = read_file("example.txt")
print(content)
More Examples

🔄 Integration with NPM

npm init -y
npm install lodash express axios
from pythonodejs import node_eval

result = node_eval("""
const _ = require('lodash');
const numbers = [1, 2, 3, 4, 5];
_.sum(numbers);
""")
print(f"Sum: {result}")

node_eval("""
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from Express running in Python!');
});

app.listen(3000, () => {
  console.log('Express server running on port 3000');
});
""")

🔧 Advanced Usage

Working with JS Objects

person = node_eval("""
const person = {
  name: 'John',
  age: 30,
  greet: function() {
    return `Hello, my name is ${this.name}`;
  }
};
person;
""")
print(person['name'])
print(person['greet']())

Passing Python → JS

process_data = node_eval("""
function processData(data) {
  return {
    processed: true,
    items: data.items.map(x => x * 2),
    message: `Processed ${data.items.length} items`
  };
}
processData;
""")

result = process_data({'items': [1, 2, 3, 4, 5]})
print(result)

✅ ToDo

  • Stable release
  • Loading ES Modules
  • Automatic npm install on dependency detection
  • Better error logging

🏗️ Building from Source

  1. Install SConstruct and Clang
  2. Run pip install -r requirements.txt
  3. Run scons

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📜 License

This project is licensed under the MIT License.

🙏 Special Thanks

A special thanks to M-logique for their contribution to this project.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages