17

I have a piece of code written in Python. I would like to put that code in a webpage. Brython seems like the simplest way to glue the two things together, but I don't have a server that can actually run code on the server side.

Does Brython require server-side code, or can I host a page using it on the cheap with (say) Dropbox?

badp
  • 11,409
  • 3
  • 61
  • 89
  • Note that independently of this issue, it currently implements only a very small and limited subset of Python (not even basic classes last I checked!). And with their approach being "try to implement a Python interpreter in JavaScript", I doubt they'll even achieve 90% core language compatibility anytime soon. –  Jan 19 '13 at 21:19
  • 1
    @delnan Curses. I'd used classes, operator overloading and all kinds of niceties... oh well. – badp Jan 19 '13 at 21:32
  • 1
    +1. Related question: Can one run django in brython? –  Jan 30 '13 at 19:50
  • @g33kz0r: no. But you can probably run it in Empythoned. – Janus Troelsen May 13 '13 at 18:29
  • 3
    **update**: As of late 2014, Brython's implementation is now a very large subset of the language - with all of the data model working, and a large part of the standard library itself. One can really confortably code like it is Python, and see the results in the browser. – jsbueno Nov 25 '14 at 20:20

4 Answers4

28

Brython is pretty new, and as such is changing rapidly. But, you can use it pretty much anywhere without server side code.

Some possibilities:

  • run locally, with no server whatsoever. You open the html file in your web browser
  • run on a free web site, without server side access, such as a blog. I use blogger, for example, to demo stuff on my blog, like this interactive breadboard
  • run on a host where you can do stuff server side. This allows you to do Ajax (same domain - check the ajax example in the gallery), and to import libraries that are stored on the server
  • integrate brython with your favorite web framework and templating engine

Just to be clear, you do not need python on your server. And if you cant link to the brython.js file, you can include the javascript in your web page.

As for the language support, it does support all the basics and does support classes, list comprehensions, and some less obvious stuff like eval, dis etc. There is a stdout and stderr that default to the web browser console, but can be redirected ( see the console on the brython site for an example of that ). It integrates fine with javascript libraries through JSObject. It has nice HTML5 support.

The english and spanish documentation are lagging a little behind the french documentation since they are translated from the original in french. They should be back in sync in the next few days. Still, if you look at the console and gallery, it gives you a taste of what is possible.

There are mailing lists linked on the brython site to provide support if you get stuck.

I would have provided more examples and links, but I'm new, so I'm limited on the hyperlinks.

Lipis
  • 21,388
  • 20
  • 94
  • 121
Francois Dion
  • 381
  • 2
  • 3
  • 1
    Congratulations on your first answer upvote, now you can post hyperlinks to your heart's content :) – badp Jan 20 '13 at 10:54
5

Brython itself seems to be completely client side, but whether that will be enough really depends on the code you wrote. It is not a full blown Python interpreter and does not have the libraries. You might want a backend to support it or use another client side solution as suggested in the comments.

Given how few real web hosters support Python, I think it is very unlikely that Dropbox would be suitable for this, in case you do need processing on the server as well.

Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48
  • Dropbox is actually a file locker service with a special folder wherein files can be accessed from the internet. It's not even a web hoster to begin with. – badp Jan 19 '13 at 21:33
  • @badp That's why I call the others *real* web hosters. Dropbox could just be abused to be a very simple one, I suppose. – Thijs van Dien Jan 19 '13 at 21:41
  • 2
    if you need the libraries, you can use [empythoned](https://github.com/replit/empythoned) (CPython on Emscripten) – Janus Troelsen May 13 '13 at 14:33
  • If it's client side, it doesn't matter what the web host supports. – Rich Nov 22 '16 at 10:26
2

Brython doesn't always work with python code, I've learned.

Something I think needs to be clarified is that while you can run brython in a very limited capacity by accessing the files locally, (because of the AJAX requirement) you can't import libraries - not even the most basic (e.g., html, time). You really need a basic web server in order to run brython.

I've found it's good for basic scripts, since my python is better than my JS. It seems to break with more complicated syntax, though.

Paul
  • 3,634
  • 1
  • 18
  • 23
  • 2
    Brython has improved since this answer. It now imports libraries even when accessing files locally, which is important if you're embedding in an offline smartphone app. – Silas S. Brown Jul 13 '17 at 18:55
2

Yes, brython is entirely cliant-side. Yes, you can use it with Dropbox. What you need to do is copy a public link to the brython.js file and place it in your html file's head tag, where the default text is:

<script src=...

place the public link in the quotes and close the script tag. So the code would look like:

<html>

    <head>
        <script src="Public Dropbox Link To The brython.js File"></script>
    </head>

    <body onload="brython()">

        <script type="text/python">
        from browser import document, alert

        def echo(event):
            alert(document["zone"].value)

        document['mybutton'].bind('click', echo)
        </script>

        <input id="zone"><button id="mybutton">click !</button>

    </body>

</html>

And this is the example they give on their page. note You don't need to have the html file on your dropbox, it can be anywhere with internet access.