Sharing a database between the backend and the browser

15 views
Skip to first unread message

Ram Rachum

unread,
May 28, 2021, 10:34:47 AM5/28/21
to pyweb-il
Hi guys!

Here's something I'm trying to do for my research. I want my Python process to be generating data of different types, that'll be stored in some kind of database or files. I then want the browser to be reading that data. I want to avoid writing a CRUD API for it. I want the Python code I write for the browser (using Brython) to access the data as naturally as the backend Python process accesses it. 

Is there a way to access databases from the browser? (In JavaScript I guess.) 

I searched and found sql.js, but I don't think it's good for me: "It uses a virtual database file stored in memory, and thus doesn't persist the changes made to the database. However, it allows you to import any existing sqlite file, and to export the created database as a JavaScript typed array." I'm not sure I want to limit my browser to just reads, but even if I did, this wouldn't be good for me. If I added more data to the database from the backend, the sql.js code in the browser wouldn't be able to pick it up by reading the database, but only by reloading the entire file and then reading it. 

Any ideas?

Omer Zak

unread,
May 28, 2021, 10:47:16 AM5/28/21
to pywe...@googlegroups.com
Hi Ram,

Have one of the processes (the backend Python process?) communicate
directly with the database.
The other process (the browser?) will communicate with the above
process via a TCP/IP socket.

You may want to read about RDA:
https://en.wikipedia.org/wiki/Remote_Database_Access
or activate mysql/psql/sqlite (the shell programs) in the backend and
have the browser communicate with them via pipes (or WebSockets?).

--- Omer Zak
--
One cannot argue with a Bayesian filter. Peter Lorand Peres
My own blog is at https://tddpirate.zak.co.il/

My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with
which I may be affiliated in any way.
WARNING TO SPAMMERS: at https://www.zak.co.il/spamwarning.html


yotam...@gmail.com

unread,
May 29, 2021, 2:51:26 AM5/29/21
to PyWeb-IL
Hi Ram, hope all is well!

Expanding on Omer's answer, you could try using gRPC in Brython, or some other RPC framework/library.

Also a few months back we were looking on my team for a similar hack but strictly in JS world (running sql queries directly from the client browser), and we weren't able to find a tool that does that well. Seems like the world insists on it being a bad idea :)

On the other hand, there are good tools that auto generate CRUD apis from a database schema, which may answer your concern of not having to write it yourself. PostgreSQL to REST can be done using https://postgrest.org/ and PostgreSQL to GraphQL Server can be done using https://www.graphile.org/postgraphile/ (We are using the latter in production and are quite happy with it).

Hopefully that helps.

Take care!
Yotam

Ram Rachum

unread,
May 29, 2021, 4:06:39 AM5/29/21
to pyweb-il
Thank you. It looks like I'm writing my own database and a minimal REST API for it. 

--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyweb-il+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyweb-il/c303600c-4b5a-4153-9130-6a56a0310948n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages