Brython: import the bs4 library and the requests library
Question:
I am creating a web application using python with the brython.js library and for my application I need to import the bs4 library and the requests library but I don’t know how.
Any idea?
<script type="text/python">
from browser import document
import bs4 #doesn't work
import requests #doesn't work
</script>
Answers:
Like in standard Python, you can install modules or packages Python in your application by putting them in the root directory, or in directories with a file __init__.py
.
Note: that modules must be encoded in utf-8; the encoding declaration at the top of the script is ignored.
For instance, the application can be made of the following files and directories:
app.html
brython.js
brython_modules.js
brython_stdlib.js
index.html
users.py
utils.py
+ app
__init__.py
records.py
tables.py
And you can run the imports:
import users
import app.records
You can use ajax
, a function in the browser module.
from browser import ajax
def read(req):
print(req.text)
ajax.get('https://example.com', oncomplete=read)
Note: You may face issues regarding CORS
I am creating a web application using python with the brython.js library and for my application I need to import the bs4 library and the requests library but I don’t know how.
Any idea?
<script type="text/python">
from browser import document
import bs4 #doesn't work
import requests #doesn't work
</script>
Like in standard Python, you can install modules or packages Python in your application by putting them in the root directory, or in directories with a file __init__.py
.
Note: that modules must be encoded in utf-8; the encoding declaration at the top of the script is ignored.
For instance, the application can be made of the following files and directories:
app.html
brython.js
brython_modules.js
brython_stdlib.js
index.html
users.py
utils.py
+ app
__init__.py
records.py
tables.py
And you can run the imports:
import users
import app.records
You can use ajax
, a function in the browser module.
from browser import ajax
def read(req):
print(req.text)
ajax.get('https://example.com', oncomplete=read)
Note: You may face issues regarding CORS