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

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?

Related

python imprt own function in html page

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>

Dynamic source code shows in browser on Windows

I'm running Windows 7, and using Google Chrome as my web browser.
This is my code:
#!/usr/bin/env python
print 'content-type: text/html\n'
print """
<!DOCTYPE html>
<html>
<head>
<title> sample </title>
</head>
<body>
"""
print 2 + 3
print """
</body>
</html>
"""
In Command Prompt, where python results in C:\Python27\python.exe, but according to this post I should use #!/usr/bin/env python instead.
When I try to run this .py code in my browser, the source code appears instead of just 5.
If someone would provide detailed instructions as to how to properly work this, I would be most grateful.
Thanks!
Okay. So of course it is not possible to run python directly in the browser but you can configure a local server to be able to run CGI scripts. Since you are on windows you could use something like a WAMP.
Try following this tutorial
https://wiki.python.org/moin/CgiScripts

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