Connection error message in firefox developer console(f12) belov. How can I connect Elastic Cloud from pyscript client. I simple connecting Pyscript CDN and importing Elastic Search library in python()
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
- from elasticsearch import Elasticsearch
<html>
<head>
<title>Matplotlib</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- from elasticsearch import Elasticsearch
</py-env>
</head>
<body>
<py-script>
# Password for the 'elastic' user generated by Elasticsearch
ELASTIC_PASSWORD = "LKQp5hTjR6KC5zTbFtHT2V9r"
# Found in the 'Manage Deployment' page
CLOUD_ID = " "
# Create the client instance
client = Elasticsearch(
cloud_id=CLOUD_ID,
basic_auth=("elastic", ELASTIC_PASSWORD)
)
# Successful response!
client.info()
# {'name': 'instance-0000000000', 'cluster_name': ...}
</py-script>
</body>
</html>
The Error
Uncaught (in promise) PythonError: Traceback (most recent call last):
pyparsing.exceptions.ParseException: Expected string_end, found 'elasticsearch' (at char 5), (line:1, col:6)
raise InvalidRequirement(
packaging.requirements.InvalidRequirement: Parse error at "'elastics'": Expected string_end
setTimeout handler*hiwire_call_bound pyodide.asm.js:14
callPyObjectKwargs pyproxy.gen.ts:360
callPyObject pyproxy.gen.ts:384
wrapper pyodide.asm.js:14
setTimeout handler*hiwire_call_bound pyodide.asm.js:14
CodePudding user response:
Your declaration for <py-env>
is incorrect. It should look like this:
<py-env>
elasticsearch
</py-env>
Import elasticsarch like this:
<py-script>
from elasticsearch import Elasticsearch
...
However, Elastic Search is not a supported package. It relies upon the Python Requests
and ssl
packages, which are supported by browsers. The Requests
package uses the operating system TCP Socket API. That API is not available in the browser. This is not a PyScript limitation, it is a security restriction in the browser for all applications.
The only solution is to modify Elastic Search to use browser-supported APIs such as the Fetch API.