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
Related
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>
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?
I am trying to get Python code to work on a web browser using the CGI module, as well as some other file modules that I am trying to import into the main program.
My initial program, which works as required, is the following:
#!C:\Users\Student\AppData\Local\Programs\Python\Python37-32\python.exe
import cgi
def htmlTop():
print("Content-type: text/html")
print()
print("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My Server Side Test</title>
</head>
<body>""")
def htmlTail():
print("""</body>
</html>""")
if __name__ == "__main__":
try:
htmlTop()
print("Hello World")
htmlTail()
except:
cgi.print_exception()
In the same folder as this program, I have a Python file called "_serverTestModules" containing:
def _serverTestFunction():
print("The file has been imported successfully")
If I then add:
import _serverTestModules
to the original file, both of which are in the same directory, and attempt to access the function from the original code, I get this error:
Traceback (most recent call last):
File "C:/xampp/htdocs/inventorysystem/_serverTest.py", line 26, in <module>
_serverTestFunction()
NameError: name '_serverTestFunction' is not defined
So basically my test program works, but as soon as I try to import functions from other files which are in the same directory (something which I need for my main project), the code fails. Is this a problem with the computer system or the network? Every other answer I have seen on Stack Overflow has either had no answer or an incorrect answer, so any help is much appreciated.
You need to use this
from _serverTestModules import _serverTestFunction
I am trying to parse an xml from an url.
So originaly my code looked like this:
from xml.dom import minidom
xmldoc = minidom.parse('all.xml')
In this case the xml what I intend to parse was on my local harddrive and it worked out perfectly!
The next step was to parse a xml from an url.
A sensormeter from allnet constantly inserts xml data into the networkt which is over the following url with a browser accessible: 192.168.60.242/xml
this is the xml:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
</head>
<body>
<form>
<textarea rows="50" cols="132"><xml><data> <devicename>ALL4000</devicename> <n0>0</n0><t0> 2.39</t0><min0> 0.00</min0><max0> 2.55</max0><l0>-55</l0><h0>150</h0><s0>102</s0> <n1>1</n1><t1> 2.53</t1><min1> 2.32</min1><max1> 10487.04</max1><l1>-55</l1><h1>150</h1><s1>102</s1> <n2>2</n2><t2> 2.55</t2><min2> 0.00</min2><max2> 2.55</max2><l2>-55</l2><h2>150</h2><s2>102</s2> <n3>3</n3><t3>-20480.00</t3><min3> 0.00</min3><max3> 5580.80</max3><l3>-55</l3><h3>150</h3><s3>0</s3> <n4>4</n4><t4>-20480.00</t4><min4> 40.96</min4><max4> 41943.04</max4><l4>-55</l4><h4>150</h4><s4>0</s4> <n5>5</n5><t5>-20480.00</t5><min5> 10.24</min5><max5> 0.08</max5><l5>-55</l5><h5>150</h5><s5>0</s5> <n6>6</n6><t6>-20480.00</t6><min6> 0.00</min6><max6>-20480.00</max6><l6>-55</l6><h6>150</h6><s6>0</s6> <n7>7</n7><t7>-20480.00</t7><min7> 0.00</min7><max7> 0.00</max7><l7>-55</l7><h7>150</h7><s7>0</s7> <n8>8</n8><t8>-20480.00</t8><min8> 336855.04</min8><max8> 1342177.28</max8><l8>-55</l8><h8>150</h8><s8>0</s8> <n9>9</n9><t9>-20480.00</t9><min9> 0.00</min9><max9> 0.00</max9><l9>-55</l9><h9>150</h9><s9>0</s9> <n10>10</n10><t10>-20480.00</t10><min10> 0.00</min10><max10> 0.00</max10><l10>-55</l10><h10>150</h10><s10>0</s10> <n11>11</n11><t11>-20480.00</t11><min11> 0.00</min11><max11> 0.00</max11><l11>-55</l11><h11>150</h11><s11>0</s11> <n12>12</n12><t12>-20480.00</t12><min12> 0.00</min12><max12> 0.00</max12><l12>-55</l12><h12>150</h12><s12>0</s12> <n13>13</n13><t13>-20480.00</t13><min13> 0.00</min13><max13> 0.00</max13><l13>-55</l13><h13>150</h13><s13>0</s13> <n14>14</n14><t14>-20480.00</t14><min14> 0.00</min14><max14> 0.00</max14><l14>-55</l14><h14>150</h14><s14>0</s14> <n15>15</n15><t15>-20480.00</t15><min15> 0.00</min15><max15> 0.00</max15><l15>-55</l15><h15>150</h15><s15>0</s15> <fn0>1</fn0><ft0>0</ft0><fs0>0</fs0> <fn1>2</fn1><ft1>0</ft1><fs1>0</fs1> <fn2>3</fn2><ft2>0</ft2><fs2>0</fs2> <fn3>4</fn3><ft3>0</ft3><fs3>0</fs3> <fn4>5</fn4><ft4>0</ft4><fs4>0</fs4> <fn5>6</fn5><ft5>0</ft5><fs5>0</fs5> <fn6>7</fn6><ft6>0</ft6><fs6>0</fs6> <fn7>8</fn7><ft7>0</ft7><fs7>0</fs7> <fn8>9</fn8><ft8>0</ft8><fs8>0</fs8> <fn9>10</fn9><ft9>0</ft9><fs9>0</fs9> <fn10>11</fn10><ft10>0</ft10><fs10>0</fs10> <fn11>12</fn11><ft11>0</ft11><fs11>0</fs11> <fn12>13</fn12><ft12>0</ft12><fs12>0</fs12> <fn13>14</fn13><ft13>0</ft13><fs13>0</fs13> <fn14>15</fn14><ft14>0</ft14><fs14>0</fs14> <fn15>16</fn15><ft15>0</ft15><fs15>0</fs15> <rn0>0</rn0><rt0>0</rt0> <rn1>1</rn1><rt1>0</rt1> <rn2>2</rn2><rt2>0</rt2> <rn3>3</rn3><rt3>0</rt3> <it0>248</it0><it1>255</it1><it2>255</it2><it3>255</it3><it4>128</it4><it5>1</it5><it6>255</it6><it7>255</it7> <date>05.08.2006</date><time>07:54:10</time><ad>1</ad><ntpsync>-1</ntpsync><i>10</i><f>0</f> <sys>18773719</sys><mem>24760</mem><fw>2.89</fw><dev>ALL4000</dev> <sensorx>5</sensorx><sensory>3</sensory> </data></xml> </textarea>
</form>
</body>
</html>
So I changed the code into this:
import urllib
from xml.dom import minidom
xmldoc = minidom.parse(urllib.urlopen('https//192.168.60.242 /xml')).read()
Unfortunatelly it does not work.
If executed, This is what gets returned:
File "/usr/lib/python2.7/urllib.py" line 86, in urlopen return
opener.open(url)
IOError: Errno socket error Errno 111 Connection refused
I hope someone can help me out on this.
thanks
Try pinging your website from your code and make sure you don't have login privileges, log in to your admin cp and make sure your xml page allows users to read or change its permissions.
Alternatively, try this-
xmlUrl= urllib2.urlopen("http://192.168.60.242/xml" )
xmlUrl_string= xmlUrl.read()
parser.parseString( xmlUrl_string)
I've been working on testing Twitter Bootstrap with CherryPy 3.2.2, and have reviewed several of the SO posts, but have been unable to successfully get Cherry to run with my configuration files. I'm getting the infamous 404 error: "NotFound: (404, "The path '/' was not found.")".
This is my file setup:
/TwitApp (application directory)
testPy.py (my test application)
config.conf (configuration file to add CSS)
global.conf (global configuration for server socket, port, etc. I think this can go in config.conf?)
/css (CSS folder)
bootstrap.min.css
/js (javascript directory)
This is my testPy.py code:
#!/usr/bin/env python
import cherrypy
class HelloWorld:
def index(self):
return '''<!DOCTYPE html><html><head>
<title>Bootstrap Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="../css/bootstrap.min.css" rel="stylesheet" media="screen">
</head><body>
<h1>Bootstrap Test</h1>
<button type="button" class="btn">Submit</button>
</body></html>'''
index.exposed = True
#I tried this example that I found in the documentation and on previous posts with the same 404 result:
#cherrypy.quickstart(HelloWorld(), "/", "config.conf")
#I tried this one when I reviewed another post on here:
cherrypy.config.update("global.conf")
cherrypy.tree.mount(HelloWorld(),"/","config.conf")
cherrypy.engine.start()
cherrypy.engine.block()
At one point the [global] section of my global.conf file was the top section of my config.conf file, but I split the two up when I started using the mount, start and block methods.
Here is my global.conf file:
[global]
server.socket_host = "127.0.0.1"
server.socket_port = 8080
log.screen: True
log.error_file: "/Users/myUser/tmp/newproject/cherrypy.error"
log.access_file: "/Users/myUser/tmp/newproject/cherrypy.access"
Here is my config.conf file:
[/]
tools.staticfile.root = "/Users/myUser/tmp/newproject/TwitApp"
[/css/bootstrap.min.css]
tools.staticfile.on = True
tools.staticfile.filename = "css/bootstrap.min.css"
This is the full error message:
404 Not Found
The path '/' was not found.
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cperror.py", line 386, in __call__
raise self
NotFound: (404, "The path '/' was not found.")
The articles I read through the most can be found here: Cherrypy returning NotFound: (404, "The path '/' was not found.") and here: Path Not Found in CherryPy. Can someone please advise what I'm doing incorrectly?
Take a look at https://github.com/btsuhako/bootstrap-cherrypy-jinja2
In the configuration files, it's easier to setup a static dir in your CherryPy app config file:
[/css]
tools.staticdir.on: True
tools.staticdir.dir: 'css'
[/js]
tools.staticdir.on: True
tools.staticdir.dir: 'js'
Final answer: I decided that I should just start troubleshooting, so I hacked down all of the CherryPy configurations and just did a basic cherrypy.quickstart(HelloWorld()) with the template I had. I was still getting the 404 error, so that meant that there was something fundamentally wrong with my code, not a config error. I kept looking through everything and then I found it. On vim, my code alignment was a little off and that's when I noticed that index.exposed = True was not set outside the index function! D'oh! I replaced it and now it's working with CSS, so anyone who's interested in learning how to implement CherryPy with CSS, there ya go!