Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Chilkat2-Python Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Chilkat2-Python) Call a JavaScript Function Returning a Boolean

See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns a boolean.

Note: This example requires Chilkat v11.4.0 or greater.

Chilkat2 Python Downloads

install with pip

pip3 install chilkat2

or download... Python Module for Windows, Linux, Alpine Linux, MacOS

import sys
import chilkat2

success = False

# This is the JavaScript function we'll call:

# function isEven(number) {
#     return number % 2 === 0;
# }

sbScript = chilkat2.StringBuilder()
sbScript.Append("function isEven(number) { return number % 2 === 0; }")

js = chilkat2.Js()

result = chilkat2.JsonObject()
result.EmitCompact = False

# Call Eval to add the function to the context's global object
success = js.Eval(sbScript,result)
if (success == False):
    # Examine the result for an exception.
    print(result.Emit())

    # Also examine the LastErrorText.
    print(js.LastErrorText)
    sys.exit()

# ------------------------------------------------------------------------------
# Call the function isEven(8)

funcCall = chilkat2.JsonObject()

# Create JSON specifying the function name and arguments

# {
#   "name": "isEven",
#   "args": [ 8 ]
# }

funcCall.UpdateString("name","isEven")
funcCall.UpdateInt("args[0]",8)

success = js.CallFunction(funcCall,result)
if (success == False):
    # Examine the result for an exception.
    print(result.Emit())

    # Also examine the LastErrorText.
    print(js.LastErrorText)
    sys.exit()

print(result.Emit())

# Output:
# {
#   "type": "bool",
#   "value": true
# }

retval = result.BoolOf("value")
print(str(retval))

# Output:
# True

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.