Search code examples
pythonbrython

Brython: how do I get packages (math, time, etc.)


So, being a big fan of Python I found Brython that may help me at building a more comprehensive Web based GUI (I wish).

I found the clock demo that I descided to try locally. It is composed of two files:

  1. The html file
  2. The python clock code

Here is the clock.html file:

    <!doctype html>
    <html>

    <head>
        <meta charset="utf-8">
        <script type="text/javascript"
            src="/brython.js">
            src="/brython_stdlib.js">
        </script>
        <script src="clock.py" type="text/python3"></script>
    </head>

    <body onload="brython();">

    </body>

    </html>

And the clock.py file which is only partial here (found on Brython.info, follow the link to get the full code):

    import time
    import math
    import datetime

    from browser import document as doc
    import browser.timer

    sin, cos = math.sin, math.cos

    ...

    show_hours()
    set_clock()

I then issued the commands:

    mkdir js
    cd js
    python -m brython --install

Finally I built a mini server to serve my files:

mini_server.py:

    from flask import Flask, send_from_directory
    app = Flask(__name__, static_folder='.')

    @app.route('/')
    def example():
            return app.send_static_file('clock.html')

    @app.route('/<name>.js')
    def js_files(name):
        return app.send_static_file('js/' + name + '.js')

    @app.route('/<path:path>')
    def all_files(path):
        return app.send_static_file(path)

    if __name__ == '__main__':
        app.debug=True
        app.run()

So I get this structure:

clock.html
clock.py
mini_server.py
js/brython.js
js/brython_stdlib.js

Now my problem is that when run my server and go to http://127.0.0.1:5000/clock.html I get:

    Error 404 means that Python module time was not found at url http://127.0.0.1:5000/time.py

Ok, I thought it would be simple to solve but looking at documentation I found only one way to solve:

    python -m brython --add_package time

But this throws:

    add package time...
    Traceback (most recent call last):
      File "C:\Program Files\Python36\lib\runpy.py", line 193, in _run_module_as_main
        "__main__", mod_spec)
      File "C:\Program Files\Python36\lib\runpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "C:\Program Files\Python36\lib\site-packages\brython\__main__.py", line 142, in <module>
        main()
      File "C:\Program Files\Python36\lib\site-packages\brython\__main__.py", line 48, in main
        package_file = package.__file__
    AttributeError: module 'time' has no attribute '__file__'

What is the problem? How or where do I get the time and the math and the datetime packages?

Seem simple but I'm now desperate for a solution.


Solution

  • Found my problem:

    My clock.html was badly pasted and the brython_stdlib.js was not loaded. The math module is part of the brython_stdlib.js file that must be loaded as a script in the html file along with brython.js. So something like this:

        <script type="text/javascript" src="/brython.js"></script>
        <script type="text/javascript" src="/brython_stdlib.js"></script>
    

    In my case my second script import command had a typo and it was not loaded (look at the clock.html file in the question). This gave out console output that thew me off to thinking that math file was missing:

    brython.js:9188 GET http://127.0.0.1:5000/time.py?v=1591284638836 404 (NOT FOUND)
    brython.js:9188 GET http://127.0.0.1:5000/time/__init__.py?v=1591284638855 404 (NOT FOUND)
    brython.js:9188 GET http://127.0.0.1:5000/Lib/site-packages/time.py?v=1591284638881 404 (NOT FOUND)
    brython.js:9188 GET http://127.0.0.1:5000/Lib/site-packages/time/__init__.py?v=1591284638909 404 (NOT FOUND)
    

    So thank you, and I hope that anyone that makes the same mistake will stumble upon this post.