Comments (16)
I was able to open the file using
[[fetch]]
-->url here
inside the pyscript.toml file,
which will fetch the file in the envirnoment, so that it can be loaded in the python code.
I have implemented in project1 and project2
from pyscript.
Excellent! For those finding their way here, starting in 2023.11.1.RC3
there is also the files feature of py-config, which has a simpler interface.
from pyscript.
You cannot read/open a file from a local disc, as the code is running in the browser and would not have access to the file system, but you can definitely download the given file over the network (using the native fetch function)
from pyscript.
@Rjtsahu So you are saying that we cannot access the abc.txt file inside the <py-script></py-script>
tags?
from pyscript.
That is exactly what he is saying, your py-script code is running in web assembly container which is separate from the files on disk.
You will need to upload/download files depending on your use case, see #111 and #124
from pyscript.
That is exactly what he is saying, your py-script code is running in web assembly container which is separate from the files on disk.
You will need to upload/download files depending on your use case, see #111 and #124
Good to know, new to front end.
from pyscript.
@ckavidas Thanks for the description.
If the file needs to be opened in a binary mode like open('model.pkl','rb')
then open_url(url)
does not provide the option of opening file with binary mode.
open('model.pkl','rb')
opens a saved ML model that was dumped using pickle module.
Is there a way to open that pickle file within <py-script></pyscript>
?
from pyscript.
@LakshmanKishore you need to load the file from http/fetch
from js import fetch
response = await fetch(URL)
please share the snippet of what you are trying to achieve, so I can tell exactly what you can do.
from pyscript.
I have a KNN ML model that was built with scikit-learn and dumped to iris.pkl file using pickle module.
<py-script>
import pickle
from pyodide.http import open_url
url = "https://raw.githubusercontent.com/LakshmanKishore/irisClassification/master/iris.pkl"
model = pickle.load(open_url(url))
#sl-sepal length pw-petal width I will get this from the html form element.
data = {"sl":Element("sl").value, "sw":Element("sw").value, "pl":Element("pl").value, "pw":Element("pw").value }
def predict(*ags, **kws):
prediction = model.predict([[float(data["sl"]),float(data["sw"]),float(data["pl"]),float(data["pw"])]])
classes = ["SETOSA","VERSICOLOR","VIRGINICA"]
answer = classes[int(prediction)]
print(answer)
</py-script>
I tried the above snippet but it was showing
JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 4, in TypeError: a bytes-like object is required, not 'str' )
Since pickle.load(file.pkl)
needs the file.pkl to be opened in binary mode, whereas open_url opens the file in str format.
Once the pkl file is loaded, I will use model.predict()
to get the predictions of the new input data.
from pyscript.
See this example
In the code you can see upload file/read file operations which include converting to raw bytes.
from pyscript.
import pickle
class StrToBytes:
def __init__(self, fileobj):
self.fileobj = fileobj
def read(self, size):
return self.fileobj.read(size).encode()
def readline(self, size=-1):
return self.fileobj.readline(size).encode()
from pyodide.http import open_url
url = "https://raw.githubusercontent.com/LakshmanKishore/irisClassification/master/iris.pkl"
model = pickle.load(StrToBytes(open_url(url)))
I was able to convert the model into bytes format using the above StrToBytes
class, but showing error when loading the model.
JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 13, in _pickle.UnpicklingError: invalid load key, '\xef'. )
I don't know whether this error is due to pkl file or the usage of the pickle inside <py-script>
tag.
from pyscript.
@LakshmanKishore can you verify if it is working in normal mode (python3 standard version), you can try to read the file directly using open
.
Let's make sure it's not a pickle file issue.
from pyscript.
Pickle file is working perfectly fine when the file is located and accessed locally, but when I tried to get the pickle file from online(like from the raw.githubusercontent.com it is showing line 13, in _pickle.UnpicklingError: invalid load key, '\xef'.
error
I'm guessing that the pickle file may be getting corrupted when we try to load the file using the url.
from pyscript.
Hey @LakshmanKishore, I'm not super knowledgeable with pickle
stuff but it seems like this is no longer related to pyodide. A quick google search returned stuff like: leftthomas/ESPCN#6.
Can you confirm this or the problem persists?
Thanks!
from pyscript.
Hello @marimeireles
I updated the pickle file.
I'm still getting the same UnpicklingError.
from pyscript.
Closing it as dup. please redirect any questions related to the topic to the documentation issue.
If you're facing a different problem or think might have encountered a bug please feel free to open a new issue.
If I misinterpreted your issue and you think it should still be open please reply here and we'll reopen it.
Thank you!
from pyscript.
Related Issues (20)
- PyTerminal not bootstrapping as expected later on ...
- allow `pyscript.fetch` to be separately awaited
- rich terminal broken in 2024.2.1+ HOT 7
- print(...) + input() does not work as expected HOT 7
- cannot use cross origin HOT 17
- TypeError: Promise.withResolvers is not a function HOT 3
- Help with building a briefcase project for the web HOT 3
- Broken registration / login process on pyscript.com HOT 9
- Use of zipped/tgz assets not working in MicroPython HOT 6
- Ka
- ModuleNotFoundError when importing module from local file using [[fetch]] in pyscript_config.toml (in Next.js / React project) HOT 16
- Unable to connect to webpages with the requests library HOT 2
- Unable to use local piodide with worker HOT 3
- Getting RangeError: WebAssembly.Memory(): could not allocate memory HOT 1
- `py-editor` shows error in `2024.5.1` HOT 4
- Passing elements to pyscript.when causes ImportError in MicroPython
- Documentation: link to development environment setup guide 404s HOT 1
- Local Pyodide Packages Tutorial: Failed to load packaging, micropip
- Adding Interactivity To the Py-Editor HOT 1
- Failed to set up Error occurred in M1 Mac : Following Developer Guideline
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyscript.