Re: [gnome-db] PyGObject for web Development

Christophe Bastin <[email protected]> Mon, 27 Oct 2014 12:15:37 +0100
Newsgroups gmane.comp.gnome.db,gmane.comp.gnome.python.devel
Message-ID <[email protected]>
With the binding, you can use Gda for web applications.
You just need to import the library.

I made a example in Python 3 with cherrypy library for the web server.

Hope it helps !

import cherrypy
from gi.repository import Gda
from gi.repository import GLib


class MyWebsite (object):

    def __init__(self):
        self.conn = None
        self.list_names = []

        self.setup_database()
        self.insert_data()
        self.get_data()

    def setup_database(self):
        self.conn = Gda.Connection(provider = 
Gda.Config.get_provider("SQLite"),
                                          cnc_string = 
"DB_DIR=.;DB_NAME=test")
        self.conn.open()

        try:
            dm = self.conn.execute_select_command("select * from user")
        except GLib.GError:
            self.conn.execute_non_select_command("create table user (id 
integer primary key, username text)")
        self.conn.close()

    def insert_data(self):
        self.conn.open()
        b = Gda.SqlBuilder(stmt_type=Gda.SqlStatementType.INSERT)
        b.set_table("user")
        b.add_field_value_as_gvalue("username", "John")

        try:
            stmt = b.get_statement()
            self.conn.statement_execute_non_select(stmt, None)
        except GLib.GError:
            print("insert error !")
        self.conn.close()

    def get_data(self):
        self.conn.open()

        try:
            # get all names
            dm= self.conn.execute_select_command("select * from user")
            iter = dm.create_iter()
            while(iter.move_next()):

                l = [Gda.value_stringify(iter.get_value_at(1))]

                self.list_names.append(l)


        except GLib.GError:
            print("Error getting data")
        self.conn.close()
        for name in self.list_names:
            print(name)



    def index(self):

        return "Hello " + str(self.list_names[0][0])
    index.exposed = True

cherrypy.quickstart(MyWebsite(), config="cherry.conf")





Le dim 26 oct 2014 à 14:32, Daniel Espinosa <[email protected]> a 
écrit :
> Is not a Django problem.
> 
> GdaData library gives you access to  database objects through a 
> Vala/GObject API and is inspired on Django. Thats all about Django.
> 
> I try, as a test, use apache mod_phython to run a GObject 
> Introspection code accessing to a database through a GDA query but 
> when I return the Gda.DataModel simply don't shows any value on 
> printing to the web page.
> 
> The point here is how can I use PyGObject on web. Could be a plus if 
> it can be used in any web framework.
> 
> El 26/10/2014 01:48, "Gergely Polonkai" <[email protected]> 
> escribió:
>> Which, in this case, makes it a Django related problem :-) I may be 
>> able to help with that, but I would need an error message and some 
>> context (and it clearly doesn't belong to these lists).
>> 
>> On 26 Oct 2014 02:00, "Daniel Espinosa" <[email protected]> wrote:
>>> Yes. In GDA, I've developed a library to easy access to Database 
>>> objects a la Django. Is developed in Vala and provides GObject 
>>> Introspection bindings.
>>> 
>>> From Python I can access to GDA and GdaData (my library) using 
>>> "from gi.repositoy import GDA, GdaData", and use to access database 
>>> objects.
>>> 
>>> But I've tried apache mod_python, and after configure a site with 
>>> that, I just found that a query data, can't be returned to  be 
>>> displayed in a routing that is called to handle a request. I call a 
>>> function when a requests with a path in the URL, it calls a 
>>> function using my library from GObject Introspection, but when 
>>> returns the data to the caller it simple don't shows any value.
>>> 
>>> 
>>> 2014-10-25 16:14 GMT-05:00 Gergely Polonkai <[email protected]>:
>>>> It’s a bit unclear to me, what exactly you want to achieve.
>>>> 
>>>> What I understand (correct me if I’m wrong) is that you wrote a 
>>>> library in Vala, which uses GDA, and you want to use your library 
>>>> from a web app written in Python.
>>>> 
>>>> If I’m right, then all you have to do is to create some GObject 
>>>> Introspection files for your library, so in Python you can simply 
>>>> use the form »from gi.repository import YourLibrary«. How to do 
>>>> this is beyond my knowledge, as I’m pretty beginner with Vala 
>>>> itself; you may want to go directly to the Vala mailing list, or 
>>>> their IRC channel.
>>>> 
>>>> On 25 October 2014 21:39, Daniel Espinosa <[email protected]> wrote:
>>>>> I would like to use PyGObject to use GDA & its Vala extensions to 
>>>>> develop a web app.
>>>>> 
>>>>> Ar there any one using this way?
>>>>> 
>>>>> If you ask why? Its because I'm developing Vala libraries and I 
>>>>> would like to use them in web applications.
>>>>> 
>>>>> 
>>>>> _______________________________________________
>>>>> gnome-db-list mailing list
>>>>> [email protected]
>>>>> https://mail.gnome.org/mailman/listinfo/gnome-db-list
>>>>> 
>>>> 
>>> 
>>> 
>>> 
>>> --
>>> Trabajar, la mejor arma para tu superación
>>> "de grano en grano, se hace la arena" (R) (en trámite, pero para 
>>> los cuates: LIBRE)

_______________________________________________
gnome-db-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/gnome-db-list