Link to home
Start Free TrialLog in
Avatar of Pace Freeman
Pace Freeman

asked on

Help with building a CherryPy web framework for a project

Hi everyone,

So I've spent the past week reading and trying to understand the python language and more importantly the CherryPy web framework.
To be honest Im getting no-where quickly...

What I'm trying to do is simply build a frontend to a Raspberry Pi project I've been developing.
The CherryPy web framework seems perfect for this as it is small and contains its own web server that would suite my needs perfectly.

An admin user attaches to the Raspberry PI AP and is immediately directed to a webpage (This I have solved quite easily).
The webpage is presented by the CherryPy web server.

I have an index.html which it returned from my CherryPy script.

import os, os.path, sys
import cherrypy

# Configuration file to access server over network and define ports
cherrypy.config.update("server.conf")

class menu(object):
    @cherrypy.expose
    def index(self):
        return open('index.html')

(Fairly simply until now I know)

What I would like to do is have buttons on my index.html that can be pressed and return os.system('mkdir boom')  (obviously my system commands will be a little more than this.. They will start and stop services.

The buttons will eventually be toggle switches, so going to the page will need to return the current status of the running process. Red if the process is not running and Green if it is running. I think I need to be interacting with jquery on my index.html page to achieve this...

This whole area is new to me and i'm finding it an uphill struggle to get to grips with..
Im not just looking for an answer to my problem, I actually want to understand how it works so that I can expand the development of the project further.  If I can just get some pointers on how to actually link the jquery script with the cherrypy script it would be a huge help.

Thanks in advance...


blupacetek
Avatar of Pace Freeman
Pace Freeman

ASKER

This is what I have so far... I know this is not pretty, I guess ill just need to build upon it...

import os, os.path, sys

import cherrypy
# Configuration file to access server over network and define ports
cherrypy.config.update("server.conf")


class menu(object):
    @cherrypy.expose
    def index(self):
        return """<html>
          <head>
            <link href="/static/css/style.css" rel="stylesheet">
          </head>
          <body>
            <form method="post" action='generate'>
              <button type="submit">Create Directory!</button>
            </form>
          </body>
        </html>"""

    @cherrypy.expose
    def generate(self,):
        os.system('mkdir yeah')
        raise cherrypy.HTTPRedirect('/')

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './public'
        }
    }
    cherrypy.quickstart(menu(), '/', conf)

Open in new window


This runs a system command, in this case make a directory called yeah, then go back to the root page.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.