python imprt own function in html page - python

knowing that with <py-config> and <py-script> can execute python code in html
However, importing own function facing error
(PY1001): Unable to install package(s) 'bread'. Reason: Can't find a pure Python 3 Wheel for package(s) 'bread'. See: https://pyodide.org/en/stable/usage/faq.html#micropip-can-t-find-a-pure-python-wheel for more information.
html script
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<b><p>title <u><label id="AAA"></label></u> </p></b>
<br>
<py-config>
packages = ["bread"]
</py-config>
<py-script>
import bread.py
bread.fun_1()
</py-script>
</body>
</html>
own python script "bread.py"
def fun_1():
print("this is bread")
(pic) the result screenshot for above code
if I import my own function in another .py script is working, but do the same thing into html not works
(pic) the result that import my own function in another .py script is fine
I read through the link system recommend link, and link but I can't find out the solution myself, that I need a hand for this problem, thanks

The reason you are getting the error is that PyScript thinks you want to import a third-party package.
To import your custom modules, use [[fetch]] in your <py-config> and replace packages with files.
<py-config>
[[fetch]]
files = ["./bread.py"]
</py-config>

Related

(python website) how to let website get output from other python script

follow by discussion Get output of python script from within python script
I create online webpage (.html) run other .py script result, but face model not found error
logically seems the python code is fine, might the setting error or other issue I don't come up myself
printbob.py
#!/usr/bin/env python
import sys
def main(args):
for arg in args:
print(arg)
if __name__ == '__main__':
main(sys.argv)
test_0109_003.html
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<b><p>title test 1.10-test_get_ print </p></b>
<br>
<py-script>
import printbob
printbob.main('arg1 arg2 arg3 arg4'.split(' '))
</py-script>
</body>
</html>
(pic 01) the result website showing
(pic 02) I put .py and .html script in WinSCP, online host system
so how to solve this problem, I locate that there might be the resaon
my winscp ip port is private that public cannot access to private's file, I'm not sure if this the reason, and if so, how to deal with it?

Import PSAW brython

I am trying to import the PSAW library in python using Brython, I have the following code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="brython.js"></script>
<script type="text/javascript" src="brython_stdlib.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
from browser import document
document <= "Hello world !"
</script>
</body>
</html>
This works correctly, however when i add PSAW like so:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="brython.js"></script>
<script type="text/javascript" src="brython_stdlib.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
from browser import document
import PushshiftAPI
document <= "Hello world !"
</script>
</body>
</html>
I get the following error in chrome:
GET http://localhost:8000/requests.py?v=1571453029466 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/requests.py
GET http://localhost:8000/requests/__init__.py?v=1571453029904 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/requests/__init__.py
GET http://localhost:8000/Lib/site-packages/requests.py?v=1571453030155 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/Lib/site-packages/requests.py
GET http://localhost:8000/Lib/site-packages/requests/__init__.py?v=1571453030212 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/Lib/site-packages/requests/__init__.py
Traceback (most recent call last):
ImportError: No module named requests
I think it means it requires other modules with Brython can't find but I'm not sure how to identify what modules are needed and how to correctly add them. Here is my file directory:
Update:
I have installed PSAW and PRAW in a virtual environment and copied all packages into the directory like so:
However I still receive the following issue when trying to import requests
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'load_module'
Uncaught Error
at Object._b_.AttributeError.$factory (eval at $make_exc (brython.js:7609), <anonymous>:41:354)
at attr_error (brython.js:6587)
at Object.$B.$getattr (brython.js:6701)
at Object.import_hooks (brython.js:13361)
at Object.$B.$__import__ (brython.js:9097)
at __import__ (brython.js:6761)
at _import_module179 (eval at run_py (brython.js:8906), <anonymous>:238:45)
at _resolve183 (eval at run_py (brython.js:8906), <anonymous>:539:84)
at method (brython.js:5321)
at load_module195 (eval at run_py (brython.js:8906), <anonymous>:1377:148)
Any idea what's happening here?
A 404 error means "file not found." Your browser is trying to access a library located at "http://localhost:8000/requests/__init__.py?v=1571453029904" (and the other locations in your traceback) but can't find it. Try visiting the URL in your browser. Does it return a file? If you can't find a file there, Brython won't be able to either. Try rearranging your filesystem until your can visit the .py file in a browser

Embedding Python Game Into HTML Using Skulpt

I have written a game in Python using the PyGame library that I am trying to embed into an HTML page to allow me to play in a web browser.
I am attempting to do this using the JavaScript library Skulpt. I have attached a test script below that successfully outputs the print statement below.
skulpt.html
<html>
<head>
<script src="assets/skulpt/skulpt.js" type="text/javascript"></script>
</head>
<body>
<textarea id="pythonCode">
print "I am python."
</textarea><br />
<pre id="output"></pre>
<script type="text/javascript">
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
var code = document.getElementById("pythonCode").value;
Sk.configure({output:outf});
eval(Sk.importMainWithBody("<stdin>",false,code));
</script>
</body>
</html>
Output of skulpt.html:
The issue that I am having is that when I use my game code instead of the simple print statement shown above it produces the error seen below;
I have included all relevant images to my web servers' directory at the correct path. I am unsure of why this error is being produced. Any help would be much appreciated, thanks!
Also, here is the attached Python game code (and a live demo of the error):
http://nicolasward.com/portfolio/skulpt.html
You have a lot of indentation on line 1 -> remember, in python, indentation always matters. Take away all those spaces/tabs on the first line and it should run.

CGI Module - Python

I found this CGI Module, its letting me use HTML tags inside a python script.
ive seen some topics in here that shows how to use it, but when im using it it doesnt works.
import cgi
print ("""
<html>
<body>
Hello
</body>
</html>
""")
and this is the output when im running the script:
<html>
<body>
Hello
</body>
</html>
how can i use this properly?
thanks.
If you have your CGI script already hooked up to a web server, you will need to emit the HTTP headers too, e.g.
print("Content-Type: text/html") # HTML is following
print() # blank line, end of headers
print ("""
<html>
<body>
Hello
</body>
</html>
""")
Note that the cgi module is not being used in any way to achieve this; just simple calls to print(). The module is useful when you want to process form data submitted by a client through a HTML form.

Package a Python app with Tide SDK doesn't work, while Launch App does

I have been working on an app for the Leap Motion, in Python, and when I tested it, it worked fine when I clicked Launch App. No app windows open, but the Python code all works fine.
However, when I package the app and launch that, it does show an app window and the Python isn't working at all... Am I missing something?
Here's the code of my index.html:
<html>
<head>
<script>
Titanium.include("PyLeapMouse.py");
Titanium.include("Geometry.py");
Titanium.include("Leap.py");
Titanium.include("LeapFunctions.py");
Titanium.include("Mouse.py");
</script>
<script type="text/python">
import Leap
import Mouse
import LeapFunctions
import time
import sys
def main():
cursor = Mouse.cursor()
listener = LeapFunctions.Listener(cursor)
controller = Leap.Controller()
print "Adding Listener."
controller.add_listener(listener)#Attach the listener
print "Press Enter to quit..."
sys.stdin.readline()
controller.remove_listener(listener)
main()
</script>
</head>
<body style="background-color:#1c1c1c;margin:0">
</body>
</html>
Try including your Python modules in the following manner:
<script type="text/python" src="PyLeapMouse.py" />
<script type="text/python" src="Geometry.py" />
<script type="text/python" src="Leap.py" />
<script type="text/python" src="LeapFunctions.py" />
<script type="text/python" src="Mouse.py" />
I know the global namespace changed from Titanium to Ti also, so if that doesn't work, you might want to try Ti.include versus Titanium.include.
Hope that works.
Include has been removed from 1.3.1 beta version
https://github.com/TideSDK/TideSDK/wiki/TideSDK-1.3.1-beta-release

Categories