How to import python library from url in Brython

713 views
Skip to first unread message

frederic kavita

unread,
Nov 17, 2017, 5:27:25 PM11/17/17
to brython

I have this code:

<script type="text/python" src="https://cdn.jcubic.pl/StringIO.py"></script>
<script type="text/python">
f = StringIO()
</script>

(StringIO came from https://svn.python.org/projects/python/trunk/Lib/StringIO.py with removed __main__ because it get executed)

and got error:

Traceback (most recent call last):
NameError: name 'StringIO' is not defined

How can I import python library from URL? Is there a way?

UPDATE found it that in Python 3 and Brython StringIO is in io module but still I would like to know how to import module from url if it's possible.


kikocorreoso

unread,
Nov 18, 2017, 5:15:12 AM11/18/17
to bry...@googlegroups.com, frederic kavita
There's this: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing.
I think you can fire a request from from front-end to your server to get the python file and then send-it to the front-end.
Other options:
-have the files you want to import on your own server.
-Pre-compile the files to js using the scripts utilities in the brython repo and then load the js files when the page is loaded.
-...


--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/cea5c4b8-be03-47bb-8f32-aa70c12604ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

frederic kavita

unread,
Nov 18, 2017, 7:13:27 AM11/18/17
to brython
can you explain in detail please this part 

-Pre-compile the files to js using the scripts utilities in the brython repo and then load the js files when the page is loaded.

kikocorreoso

unread,
Nov 18, 2017, 1:53:11 PM11/18/17
to bry...@googlegroups.com, frederic kavita


On 18/11/17 13:13, frederic kavita wrote:
can you explain in detail please this part 

-Pre-compile the files to js using the scripts utilities in the brython repo and then load the js files when the page is loaded.
You can add your python libs to the Lib/ folder:
https://github.com/brython-dev/brython/tree/master/www/src/Lib

And then run 'make_dist.py' on the scripts/ folder:
https://github.com/brython-dev/brython/tree/master/scripts
https://github.com/brython-dev/brython/blob/master/scripts/make_dist.py

This script will create 'brython.js' and a 'brython_stdlib.js' files and brython_stdlib.js should include your python modules and so you should be able to import them from a script in the page if you include:
<script type="text/javascript" src="src/brython.js"></script>
<script type="text/javascript" src="src/brython_stdlib.js"></script>

I hope it makes sense.

André

unread,
Nov 18, 2017, 4:03:22 PM11/18/17
to brython
There are at least two ways:

1. if you can put your python files on your server (like I do), you can specify where to look for them.  Here's how I do it


(the content of that line currently is: brython({debug:1, pythonpath:[RUR.BASE_URL + '/src/python']});

RUR.BASE_URL is a variable that I set to a different value when I change the location of the base file for testing, or putting on a different server; you can ignore it.)

Here's an example of one such file I load up


(in case I edit the file at some time, that line currently is: <script type="text/python3" src="src/python/init_reeborg.py"></script>)

Inside that file, I use the following that was suggested to me by someone on this list to optimize other imports


2. If you need to import files from a completely different location, you need to worry about Cross-origin resource sharing as Kiko mentioned in another reply.  I do this as well inside my program, but use a modified version of jquery to do so, together with a third party server acting as an intermediary. If you need to do something like this, I can point out what I do.

Olemis Lang

unread,
Nov 18, 2017, 5:07:21 PM11/18/17
to bry...@googlegroups.com
Hi !

There are a number of ways to import modules in Brython ranging from
individual js, .pyc.js , and .py files up to modules bundled in .vfs
archives.

On 11/18/17, André <andre....@gmail.com> wrote:
>
[...]
>
> 2. If you need to import files from a completely different location, you
> need to worry about Cross-origin resource sharing as Kiko mentioned in
> another reply. I do this as well inside my program, but use a modified
> version of jquery to do so, together with a third party server acting as an
> intermediary. If you need to do something like this, I can point out what I
> do.
>

It's true this is the current situation . Nevertheless IMHO there
should be an easy way to do this, and Brython should do its best
effort to (silently) handle all this .

--
Regards,

Olemis - @olemislc

Apache™ Bloodhound contributor
http://issues.apache.org/bloodhound
http://blood-hound.net

Brython committer
http://brython.info
http://github.com/brython-dev/brython

SciPy Latin America - Cuban Ambassador

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

Pierre Quentel

unread,
Nov 19, 2017, 9:36:30 AM11/19/17
to brython
[...]
>
> 2. If you need to import files from a completely different location, you
> need to worry about Cross-origin resource sharing as Kiko mentioned in
> another reply.  I do this as well inside my program, but use a modified
> version of jquery to do so, together with a third party server acting as an
> intermediary. If you need to do something like this, I can point out what I
> do.
>

It's true this is the current situation . Nevertheless IMHO there
should be an easy way to do this, and Brython should do its best
effort to (silently) handle all this .

There is a more simple solution, it is to add the location of the script directory to sys.path :

import sys
sys.path.append("http://localhost:8081")

# import a script accessible by http://localhost:8081/external.py
import external

You can test this locally by running this server and putting a script external.py in the same directory :

    from http.server import HTTPServer, SimpleHTTPRequestHandler

    class Handler(SimpleHTTPRequestHandler):
   
        def end_headers(self):
            self.send_header("Access-Control-Allow-Origin", "*")
            super().end_headers()
   
    port = 8081
    print('HTTP Server on port {}'.format(port))
    HTTPServer(('', port), Handler).serve_forever()


André

unread,
Nov 19, 2017, 11:09:33 AM11/19/17
to brython
This works for script that one "owns".  In my application, users can load scripts from anywhere and I cannot use the technique you describe.
(See https://aroberge.blogspot.ca/2016/12/reeborgs-answer-to-pip.html for a more detailed explanation.)  


André
 

Pierre Quentel

unread,
Nov 19, 2017, 12:06:57 PM11/19/17
to brython
You are right, of course the server must support CORS requests ; with the server on herokuapp.com, the technique can still be used to implement install_extra:

import sys

def install_extra(url):
    sys.path.append(f"http://cors-anywhere.herokuapp.com/{url}")

install_extra("http://brython.info/src/Lib/site-packages/")

import highlight

I believe that this has great potential for the distribution of Brython third party modules, probably more than trying to host them on PyPI.

The problem with adding a path to sys.path is that it will be used for all imports, so that more locations will have to be tested, with blocking Ajax calls ; we must find a less expensive way of telling a Brython application to get some specific modules in some specific locations, and forget about these locations for other imports.

Reply all
Reply to author
Forward
0 new messages