pyjamas - run python script onclick of button

I was designing a web application where i need a simple button to run a bunch of python code resides in Unix system and to show log messages of running scripts to my web application. This can be done in many ways, but i wanted to stick on python and its supported modules.
So, last few days i spent time on learning pyjamas and to configure pyjamas with jsonrpc and mod-python. That really kept me interested as it was totally new to me and lerning pyjamas (i was thinking ot it since a year!!!) is great to me.
I have gone through all sites, tutorials and everything but didn't get any good way to do it on single page and i am sure its really difficult for new programmer to do the same(examples are just to know this can be done but not well explained).

So, i am just writing down things i did to configure pyjamas, jsonrpc and mod python.
The first thing to do is, install jsonrpc, simplejson, mod_python(configured with apache) and pyjamas.

First i design frontend of my application using pyjamas.
1. create a folder VLayouts, then paste following code to VLayouts.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        import pyjd # this is dummy in pyjs
        from pyjamas.ui.RootPanel import RootPanel
        from pyjamas.ui.SimplePanel import SimplePanel
        from pyjamas.ui.Image import Image
        from pyjamas.ui.Label import Label
        from pyjamas.ui import HasAlignment
        from pyjamas.ui.Button import Button
        from pyjamas.ui.CheckBox import CheckBox
        from pyjamas.ui.VerticalPanel import VerticalPanel
        from pyjamas.ui.HorizontalPanel import HorizontalPanel
        from pyjamas.ui.HTML import HTML
        from pyjamas.ui.DockPanel import DockPanel
        from pyjamas.ui import HasAlignment
        from pyjamas.ui.FlowPanel import FlowPanel
        from pyjamas.ui.HTMLPanel import HTMLPanel
        from pyjamas.ui.ScrollPanel import ScrollPanel
        from pyjamas.ui.DisclosurePanel import DisclosurePanel
        from pyjamas import DOM, Window
        from pyjamas.ui.TextBox import TextBox
        from pyjamas.ui.PasswordTextBox import PasswordTextBox
 
        from FileOpenDlg import FileOpenDlg
        import Utils
        from pyjamas.JSONService import JSONProxy
 
 
        class VLayoutsJSONProxy(JSONProxy):
            def __init__(self):
            ## services/vlayouts.py counts from output folder
            JSONProxy.__init__(self, "services/vlayouts.py", ["callMethod","test"])
            #JSONProxy.__init__(self, "./services/vlayouts.py", ["callMethod","test"])
 
        class VLayoutsService:
            def __init__(self, callback):
            self.callback = callback
            self.proxy = VLayoutsJSONProxy()
 
            def test(self):
            self.proxy.test(self)
 
            def callMethod(self,cmd):
            Window.alert("inside VLayoutsService: callMethod")
            self.callback.showStatus("Calling Method!!!")
            a = self.proxy.callMethod("ls -lth", self)
            self.callback.showStatus(a)
 
            def onRemoteResponse(self, response, request_info):        
            Window.alert("inside VLayoutsService: onRemoteResponse")
            Window.alert(dir(request_info))
            Window.alert(request_info.method)
            Window.alert(request_info.handler)
            Window.alert(response)
            if request_info.method == "callMethod":
                self.callback.showStatus("Called method %s" % request_info.method)
            else:
                self.callback.showStatus(""" REQ METHOD = %s RESP %s """ % (request_info.method,response)) 
 
            def onRemoteError(self, code, errobj, request_info):
            Window.alert("inside VLayoutsService: onRemoteError")
            Window.alert(request_info)
            message = errobj['message']
            Window.alert(message)
            if code != 0:
                self.callback.showStatus("HTTP error %d: %s" % (code, message))
            else:
                json_code = errobj['code']
                self.callback.showStatus("JSONRPC Error %s: %s" % (json_code, message))
 
        class VLayouts(SimplePanel):
            def __init__(self):
            SimplePanel.__init__(self)
            self.VLayoutsService = VLayoutsService(self)
 
            def onModuleLoad(self):
            text="Vivek Sharma"
            self.status = Label()
            contents = HTML(text)
            scroller = ScrollPanel(contents, StyleName="ks-layouts-Scroller")
            panel = DockPanel(BorderWidth=4, Padding=20,Width="100%",Height="100%",
                          HorizontalAlignment=HasAlignment.ALIGN_CENTER,
                          VerticalAlignment=HasAlignment.ALIGN_MIDDLE)
 
            north = Image("./myImage.jpeg",Width="800px", Height="100px",)
            north.addClickListener(getattr(self, "onImageClicked"))
 
            #north  = Label("Image should be placed here")
            west = Button("RUN FUNCTION", self.onButtonClick)
            fileLocation = "logfile.log.txt"
            center = FileOpenDlg(fileLocation = fileLocation)
            ##center = Button("SHOW LOG", self.openLogFile)
            #east   = Label("East")
            #south  = Label("South")
 
            panel.add(north,  DockPanel.NORTH)
            panel.add(west,   DockPanel.WEST)
            panel.add(center, DockPanel.CENTER)
            #panel.add(east,   DockPanel.EAST)
            #panel.add(south,  DockPanel.SOUTH)
            panel.setCellHeight(north, "100px")
            panel.setCellHeight(center, "400px")
            panel.setCellWidth(center, "800px")
            panel.setCellWidth(west, "200px")
 
            panel.setCellVerticalAlignment(west, '100px')#HasAlignment.ALIGN_TOP)
 
            self.add(panel)
            #Window.alert("Hello, AJAX!")
            panel2 = VerticalPanel()
            panel2.add(self.status)
 
            RootPanel().add(panel2)
            RootPanel().add(panel)
 
            def showStatus(self, msg):
            self.status.setText(msg)
 
            def onButtonClick(self,sender):
            Window.alert("onButtonClick function called")
            self.VLayoutsService.callMethod("ls -lth")
 
            def openLogFile(sender):
            fileLocation = "logfile.log.txt"
            dlg = FileOpenDlg(fileLocation = fileLocation)
            dlg.show()
 
            def onShow(self):
            pass
 
            def onImageClicked(self):
            Window.alert("Vivek Sharma!!!")
 
            def makeLabel(self, caption):
            html = HTML(caption)
            html.setStyleName("ks-layouts-Label")
            return html
 
 
        if __name__ == '__main__':
            pyjd.setup("./VLayouts.html")
            app = VLayouts()
            app.onModuleLoad()
            pyjd.run()


configuration i did in /opt/vivek/apache/conf/httpd.conf

        <Directory "/opt/vivek/apache/cgi-bin">
            AddHandler default-handler .jpeg .png .css .html .py .js .txt
            PythonHandler jsonrpc
            AllowOverride All
            Options Indexes FollowSymLinks
            Order allow,deny
            Allow from all
        </Directory>

Now, create .htaccess in /opt/vivek/apache/cgi-bin/VLayouts/public/services folder with content
        Options FollowSymLinks
        PythonPath "['/opt/vivek/apache/cgi-bin/VLayouts/output/services']+sys.path"
        AddHandler mod_python .py
        PythonHandler vlayouts
        PythonDebug On

and check the changes in /opt/vivek/apache/cgi-bin/VLayouts/output/services/.htaccess , or directly create .htaccess here only with above content

create vlayouts.py in /opt/vivek/apache/cgi-bin/VLayouts/public/services folder with following content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        #! /usr/bin/env python
 
        import logging
        logging.basicConfig(filename="/home/vivek/Desktop/contactjson.log", level=logging.DEBUG)
        logging.debug("OLDLoading contact service")
 
        from test import AB
 
 
        class Service:
            def callMethod(self, msg):
            return "msg"
 
 
        if __name__ == '__main__':
            # this is if JSONService.py is run as a CGI
            from jsonrpc.cgihandler import handleCGIRequest
            handleCGIRequest(Service())
        else:
            # this is if JSONService.py is run from mod_python:
            # rename .htaccess.mod_python to .htaccess to activate,
            # and restart Apache2
            from jsonrpc.apacheServiceHandler import handler



run as /home/vivek/Desktop/TGZS/pyjamas-0.7/bin/pyjsbuild VLayouts.py




Leave comments

authimage

Copyright(c) 2017 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved