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>
Asked By: Blackjack

||

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
Answered By: DogCoding

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

Answered By: ajskateboarder
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.