py2vega-ts
v0.1.2
Published
Convert Python code to Typescript's Vega expressions.
Readme
py2vega-ts 🐍 -> 📊
A TypeScript library that converts Python code to Typescript's Vega expressions with the help of py-ast
Installation
npm install py2vega-tsDevelopment
Setup
# Install dependencies
npm install
# Build
npm run buildTesting
# Run tests
npm run testLinting
# install pre-commit hooks
pre-commit install
# fix linting issues
pre-commit run --all-filesQuick Start
import { py2vega } from 'py2vega-ts';
// Simple comparison
const vega = py2vega('datum.value > 100');
console.log(vega);
// Output: datum.value > 100
// Ternary operator
const colored = py2vega("'red' if datum.status == 'alert' else 'green'");
console.log(colored);
// Output: ((datum.status == 'alert') ? 'red' : 'green')
// Complex expression
const complex = py2vega('abs(datum.value) * 2 if datum.active else 0');
console.log(complex);
// Output: (datum.active ? (abs(datum.value) * 2) : 0)📖 Supported Python Syntax
Data Access
datum.field_name
datum.magnitude
datum.coordinates.xComparisons
datum.value > 100
datum.value < 50
datum.value == 5
datum.value != 10
datum.value >= 100
datum.value <= 50
datum.a < datum.bArithmetic Operations
datum.a + datum.b
datum.a - datum.b
datum.a * datum.b
datum.a / datum.b
datum.a % datum.b
-datum.valueLogical Operations
datum.a > 5 and datum.b < 10
# datum.a > 5 && datum.b < 10
datum.status == "A" or datum.status == "B"
# datum.status == "A" || datum.status == "B"
not (datum.value > 100)
# !(datum.value > 100)Ternary Operator (If-Else)
42 if datum.value > 100 else 10
# datum.value > 100 ? 42 : 10
# Nested ternaries
'red' if datum.severity > 8 else 'yellow' if datum.severity > 5 else 'green'
# datum.severity > 8 ? 'red' : datum.severity > 5 ? 'yellow' : 'green'Related Projects
- vega2ol - Convert Vega expressions to OpenLayers
- py2vega - Python package for this functionality
- JupyterGIS - Web-based GIS in Jupyter
Made with ❤️ for Jupyter lovers and geospatial enthusiasts.
