Dynamic source code shows in browser on Windows - python

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

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?

Python Eel: "Access Denied"

I'm trying to use Eel to make a simple application, but when I run this simple code:
import eel
from os import getcwd
eel.init("web")
eel.start(getcwd() + "\\main.html")
It gives me this error:
If it's important, here is the html code, just a placeholder to test:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<body>
<h1>Test</h1>
</body>
</html>
I've tried changing ownership of the file to 'Everyone' and giving full access, but that didn't do anything. I couldn't find anything about this online when I searched for it, so if anyone knows how to fix this, please let me know. TIA!
This app.py worked for me:
import eel
if __name__ == '__main__':
eel.init(".")
eel.start("main.html")
With my directory structure:
/project
|app.py
|main.html
With the code you posted, you would need a directory structure like:
/project
|app.py
/web
|main.html
I just restarted my vs code, and it's worked for me.

How to import js modules in Python with Transcrypt

I have a very Python-ic script which compiles with Transcrypt, but the issue is that the one outside dependency I have is that I need to import google-cloud-bigquery. This obviously throws an error during the transpiling process, but it seems that the API is available in JavaScript (which is my target compilation) via <script src="https://apis.google.com/js/client.js"> But Transcrypt transpiles my index.py file, and I can't just place this JS script within the Python file (that I know of), so how do I implement it?
I know that other modules, such as Numscrypt, are available through Transcrypt but how do you actually add the module within the Python file?
You will need to use the JavaScript version of the library, and putting the import in a <script> tag of the HTML file as previously described is the easiest way. Since the library will be in the global namespace at that point, you can make calls to it from anywhere in your Python program.
If you are using a bundler like Parcel or Webpack and use npm to store the libraries locally, you can use the Node.js require() function and assign it to a Python variable like:
fabric = require('fabric')
Otherwise, if you need to load the JS library from a hosted location and want to do it from within Python, you can do it with JavaScript using a Transcrypt pragma compiler directive like this as mentioned in the Transcrypt docs:
fabric = __pragma__ ('js',
'''
(function () {{
var exports = {{}};
{} // Puts fabric in exports and in global window
delete window.fabric;
return exports;
}}) () .fabric;
''',
__include__ ('com/fabricjs/fabric_downloaded.js')
)
Load the JavaScript library in the HTML page your Transcrypt code hangs off from. Then you should be able to access the top level object of Google's JavaScript Client Library in your Transcrypt module.
I am not familiar with Google's JavaScript Client Library, but from glancing at the doco I guess that gapi is the main object client.js exposes.
Schematically you could do something like this: First the HTML file index.html:
<html>
<head>
<meta charset="utf-8">
... other google scripts you might need ...
<script src="https://apis.google.com/js/client.js"></script>
</head>
<body>
... here your page elements...
<script type="module">import * as main from './__target__/your_google_stuff.js';</script>
</body>
</html>
Then in the same directory have the Transcrypt module your_google_stuff.py:
def do_your_stuff():
def auth_callback(res):
if res and res.error is None:
# Authenticated!
...do your stuff after authentication here
# here do the authentication, etc
gapi.client.setApiKey(YOUR_API_KEY) # Note that gapi should be globally available here!
gapi.auth.authorize({
'client_id': YOUR_ID,
... more atributes here...
}, auth_callback)
gapi.load(..., do_yor_stuff)
Compile your_google_stuff.py with Transcrypt and view your results by serving index.html.

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.

Categories