pulse.js
Namespaces JavaScript namespace objects for transpiled Python code
JavaScript namespace objects that provide static functions and properties. These don't use new - they're just containers for related functions.
from pulse.js import Math, JSON , console, window, document, navigator
Or import as modules for aliased access:
import pulse.js.math as Math
import pulse.js.json as JSON
import pulse.js.console as console
Mathematical functions and constants.
from pulse.js import Math
@ps.javascript
def example ():
x = Math.floor( 3.7 ) # -> Math.floor(3.7) -> 3
y = Math.random() # -> Math.random()
r = Math.sqrt( 16 ) # -> Math.sqrt(16) -> 4
Constant Description PIPi (~3.14159) EEuler's number (~2.71828) LN2Natural log of 2 LN10Natural log of 10 LOG2EBase-2 log of E LOG10EBase-10 log of E SQRT1_2Square root of 1/2 SQRT2Square root of 2
Function Description floor(x)Round down ceil(x)Round up round(x)Round to nearest integer trunc(x)Remove fractional part fround(x)Round to 32-bit float
Function Description abs(x)Absolute value sign(x)Sign (-1, 0, or 1) max(*values)Maximum value min(*values)Minimum value pow(x, y)x raised to power y sqrt(x)Square root cbrt(x)Cube root hypot(*values)Square root of sum of squares imul(x, y)32-bit integer multiplication clz32(x)Leading zero bits in 32-bit int
Function Description exp(x)e raised to power x expm1(x)exp(x) - 1 log(x)Natural logarithm log10(x)Base-10 logarithm log2(x)Base-2 logarithm log1p(x)log(1 + x)
Function Description sin(x)Sine cos(x)Cosine tan(x)Tangent asin(x)Arcsine acos(x)Arccosine atan(x)Arctangent atan2(y, x)Arctangent of y/x sinh(x)Hyperbolic sine cosh(x)Hyperbolic cosine tanh(x)Hyperbolic tangent asinh(x)Inverse hyperbolic sine acosh(x)Inverse hyperbolic cosine atanh(x)Inverse hyperbolic tangent
Function Description random()Random number in [0, 1)
JSON parsing and stringifying.
import pulse.js.json as JSON
@ps.javascript
def example ():
text = JSON .stringify(data) # -> JSON.stringify(data)
obj = JSON .parse(text) # -> JSON.parse(text)
Parse a JSON string into a JavaScript value.
JSON .parse(text)
JSON .parse(text, reviver)
Parameter Description textJSON string to parse reviverOptional (key, value) -> value transform function
Convert a value to a JSON string.
JSON .stringify(value)
JSON .stringify(value, replacer)
JSON .stringify(value, replacer, space)
Parameter Description valueValue to convert replacerOptional array of keys or (key, value) -> value filter spaceOptional indentation (number of spaces or string)
import pulse.js.json as JSON
@ps.javascript
def format_json (data):
# Pretty print with 2-space indent
return JSON .stringify(data, None , 2 )
@ps.javascript
def parse_dates (text):
# Parse with date revival
def reviver (key, value):
if key == "date" :
return Date(value)
return value
return JSON .parse(text, reviver)
Browser console logging.
from pulse.js import console
@ps.javascript
def example ():
console.log( "Hello" ) # -> console.log("Hello")
console.error( "Error!" ) # -> console.error("Error!")
Or import functions directly:
from pulse.js.console import log, error, warn
@ps.javascript
def example ():
log( "message" ) # -> console.log("message")
Function Description log(*data)General logging info(*data)Informational message warn(*data)Warning message error(*data)Error message debug(*data)Debug message
Function Description dir(item, options?)Display object properties dirxml(*data)Display XML/HTML representation table(data, columns?)Display as table
Function Description group(*label)Start collapsed group groupCollapsed(*label)Start expanded group groupEnd()End current group
Function Description time(label?)Start timer timeEnd(label?)End timer and log timeLog(label?, *data)Log current timer value
Function Description assert_(condition, *data)Assert condition (note underscore) clear()Clear console count(label?)Log call count countReset(label?)Reset counter trace(*data)Log stack trace
Browser window object - global properties and methods.
from pulse.js import window
@ps.javascript
def example ():
width = window.innerWidth # -> window.innerWidth
window.alert( "Hello!" ) # -> window.alert("Hello!")
Property Description innerWidthViewport width innerHeightViewport height outerWidthWindow width including chrome outerHeightWindow height including chrome
Property/Function Description scrollXHorizontal scroll position scrollYVertical scroll position scrollTo(x, y)Scroll to position scrollBy(x, y)Scroll by amount
Function Description alert(message)Show alert dialog confirm(message)Show confirm dialog, return bool prompt(message, default?)Show prompt dialog, return string or None
Function Description setTimeout(handler, timeout?, *args)Schedule callback, return timer ID clearTimeout(timerId)Cancel timeout setInterval(handler, timeout?, *args)Schedule repeating callback clearInterval(intervalId)Cancel interval
Function Description requestAnimationFrame(callback)Schedule before next repaint cancelAnimationFrame(requestId)Cancel animation frame
Function Description addEventListener(type, listener, options?)Add event listener removeEventListener(type, listener, options?)Remove event listener dispatchEvent(event)Dispatch event
Function Description open(url?, target?, features?)Open new window close()Close window focus()Focus window blur()Remove focus
Property Description locationURL and navigation historyBrowser history navigatorBrowser info documentDocument object localStorageLocal storage sessionStorageSession storage performancePerformance API devicePixelRatioPixel density
Function Description getSelection()Current text selection getComputedStyle(element, pseudo?)Computed styles matchMedia(query)Media query matching atob(encoded)Decode base64 btoa(data)Encode base64 print_()Open print dialog (note underscore) postMessage(message, targetOrigin, transfer?)Post message to window
from pulse.js import window
@ps.javascript
def handle_resize ():
def on_resize (event):
console.log( f "New size: { window.innerWidth } x { window.innerHeight } " )
window.addEventListener( "resize" , on_resize)
# Return cleanup function
return lambda : window.removeEventListener( "resize" , on_resize)
Browser document object - DOM access and manipulation.
from pulse.js import document
@ps.javascript
def example ():
el = document.querySelector( "#app" ) # -> document.querySelector("#app")
document.title = "New Title" # -> document.title = "New Title"
Property Description bodyDocument body element headDocument head element documentElementRoot html element activeElementCurrently focused element titleDocument title readyStateLoading state ("loading", "interactive", "complete") cookieDocument cookies referrerReferring URL URLDocument URL domainDocument domain fullscreenElementCurrent fullscreen element
Function Description querySelector(selectors)First matching element querySelectorAll(selectors)All matching elements getElementById(id)Element by ID getElementsByClassName(names)Elements by class getElementsByTagName(name)Elements by tag getElementsByName(name)Elements by name attribute
Function Description createElement(tagName, options?)Create element createTextNode(data)Create text node createDocumentFragment()Create fragment createComment(data)Create comment
Function Description addEventListener(type, listener, options?)Add event listener removeEventListener(type, listener, options?)Remove event listener dispatchEvent(event)Dispatch event
Function Description hasFocus()Check if document has focus getSelection()Current selection importNode(node, deep?)Import node from another document adoptNode(node)Adopt node from another document exitFullscreen()Exit fullscreen mode
from pulse.js import document, console
@ps.javascript
def highlight_links ():
links = document.querySelectorAll( "a" )
for link in links:
link.style.backgroundColor = "yellow"
console.log( f "Highlighted { links.length } links" )
Browser and device information.
from pulse.js import navigator
@ps.javascript
def example ():
lang = navigator.language # -> navigator.language
online = navigator.onLine # -> navigator.onLine
Property Description userAgentUser agent string languageBrowser language languagesPreferred languages platformPlatform string vendorBrowser vendor appNameApplication name appVersionApplication version onLineOnline status cookieEnabledCookies enabled pdfViewerEnabledPDF viewer available hardwareConcurrencyCPU core count maxTouchPointsMax touch points deviceMemoryDevice memory (GB, may be undefined)
Property Description clipboardClipboard API mediaDevicesMedia devices API permissionsPermissions API serviceWorkerService Worker API geolocationGeolocation API storageStorage API
Function Description vibrate(pattern)Vibrate device share(data)Web Share API canShare(data?)Check if sharing supported sendBeacon(url, data?)Send beacon request
from pulse.js import navigator, console
@ps.javascript
def check_environment ():
console.log( f "Language: { navigator.language } " )
console.log( f "Online: { navigator.onLine } " )
console.log( f "Cores: { navigator.hardwareConcurrency } " )
if navigator.canShare():
console.log( "Web Share API available" )