0

I am trying to make simple webpage that will read data from SQLite file and show it in webpage, it is very simple I want to do 3 SQL query and show result.

I have idea to use Brython for it, because it look easier that to make Django/Flask app,

This is my code:

<html>
    <head>
        <script src="/brython.js"></script>
    </head>

    <body onload="brython('debug_mode')"> <!-- for debug --!>

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

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

            document['mybutton'].bind('click',echo)

            document['text'] <= html.P("testing")
            document['text'] <= html.P("testing 222")

            # get holidays for today
            import sqlite3
            import sys

            sqlite3_connection = sqlite3.connect('test.db')

            with sqlite3_connection:

                sqlite3_cursor = sqlite3_connection.cursor()    
                sqlite3_cursor.execute('SELECT SQLITE_VERSION()')

                data = sqlite3_cursor.fetchone()

                document['text'] <= html.P("SQLite version: %s" % data) 
        </script>

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

        <div id="text">

        </div>

    </body>
</html>

This is not working, problem is Brython does not have sqlite3 implementation by default.

Is there some easy way to add sqlite3 to Brython ?

I have seen that there has been some Google summer of Code project for it, but have not found results anywhere.

WebOrCode
  • 6,852
  • 9
  • 43
  • 70
  • 1
    Since Brython runs in the browser, you should be looking at [local storage](http://diveintohtml5.info/storage.html) instead. – Martijn Pieters Mar 15 '15 at 12:13
  • I would not be intimidated by Flask. Check out this example using Flask and sqlite3 to implement a simple application: https://github.com/mikedll/flasksqlitedemo – chishaku Mar 15 '15 at 12:14
  • 1
    I'd say Brython would be *harder*, since you now cannot share that data between different browsers for the same user. Flask and Django are still just Python, while Brython is a special-purpose language using Python syntax. As such a whole host of web-related Python tutorials *will not work* on Brython. – Martijn Pieters Mar 15 '15 at 12:18
  • @MartijnPieters I need just reading of SQLite file, same file for every user. – WebOrCode Mar 15 '15 at 12:33
  • @chishaku I think that I will go with Flash. My initial idea was to use Flask, but then I remember Brython, and 2h later back to Flask. I never deployed Flask, that is why I liked Brython. – WebOrCode Mar 15 '15 at 12:33

2 Answers2

1

As Martijn put clearly int he comments - what you are trying to do would be far harder than having a minimal Python app to serve your data: Brython is intended as a client-side script language - and although it is surprisingly close to Python and getting better by the day, it is still client side.

Data on your databse, be it sqlite or anyother, lies on the server side - so at minimum you'd have to set-up a webservice to performe the database queries as needed.

Even if you want a "local" application taht would just use the browser for the UI, the cleint side brython can't get to the filesystem in order to read the database file. (This if brython did implement the sqlite3 Python module at all, which as you checked, it does not).

And anyway, coding in Brython would require you to actually have Python code program to live update the page using DOM, which will typically be more complicated than simply have a static view in Flask or other micro-framework, which will make a DB query and render a template. Moreover, Brython does not have standard (or at the momement neither "non standard") frameworks which can help templating a page and doing other niceties mature frameworks have - even though you have full Python3 string formatting, there is logic separation and flow control in templates that you'd have to reinvent.

So the hint is: just pick a nice flask/web2py/bottle tutorial somewhere, and get going. When you think you have a good static web app, add Brython to make it dynamic.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

After more research and emails to Billy Earney. Bill is person behind Brython.

sqlite3 is currently not added in Brython.

Response from him:

We were not accepted for GSOC 2014, and as a consequence SQLite3 has not been ported to Brython yet. It is still on our list of things to do.

WebOrCode
  • 6,852
  • 9
  • 43
  • 70