Richard
Richard

Reputation: 21

Need to write simple form data collection tool in python for local usage

I need to write a simple python application for data collection by a form. The form itself just needs

The app will be run locally and the data should be saved locally in a plain csv file. No networking needed.

I've found that there many different ways to solve this problem:

Of course there definately are ways I haven't found yet. The main focus is simplicity and I need a pointer to the right direction: How can I implement this so that for example even unexperienced developers may add or change a field later?

Upvotes: 1

Views: 7683

Answers (2)

jmunsch
jmunsch

Reputation: 24099

The Simplest Form I can think of save it as index.html on the desktop then open it in your browser:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myfirstform" name='hello'>
    <div>
        <label for="name">Name:</label>
        <input type="text" id="name" name="user_name" />
    </div>
    <div>
        <label for="mail">E-mail:</label>
        <input type="email" id="mail" name="user_mail" />
    </div>
    <div>
        <label for="msg">Message:</label>
        <textarea id="msg" name="user_message"></textarea>
    </div>
    <div>
    <select id="some_dropdown1" name="some_dropdown">
        <option value="" disabled="disabled" selected="selected" >Please select a name</option>
        <option value="1">One</option>
        <option value="2">Two</option>
    </select>
    </div>
</form>

<input type='button' value='Submit form' onclick='submitDetailsForm()' />

<script>
    function submitDetailsForm() {
      var f = $("#myfirstform").serialize();
      fetch('http://localhost:8080/?'+ f)
        .then(r=> r.json())
        .then(j=> {
            console.log(j)
        })
    }
</script>

And the server save it as server.py and run python server.py:

from http.server import BaseHTTPRequestHandler, HTTPServer
import urlparse, json

# python2
# from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer


class HandleRequests(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        # this line is important to understand for CORS
        self.send_header('Access-Control-Allow-Origin', '*')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        query_parms = urlparse.urlparse(self.path).query
        parsed = urlparse.parse_qs(query_parms)
        parsed['server_message'] = 'we gottem'
        self.wfile.write(json.dumps(parsed))

host = 'localhost'
port = 8080
HTTPServer((host, port), HandleRequests).serve_forever()

IMHO, this is as simple as it gets, a python builtin, and jquery, which is basically an internet builtin.

Example input usage with response from server:

no form validation, but super simple form

Example output from server:

enter image description here

For more advanced usage consider:

    def do_POST(self):
        # Doesn't do anything with posted data
        self._set_headers()
        content_len = int(self.headers.getheader('content-length', 0))
        post_body = self.rfile.read(content_len)
        self.wfile.write("received post request:<br>{}".format(post_body))

    def do_PUT(self):
        self.do_POST()

I leave figuring out how to write to a csv/tsv file.

Figure out how to write a basic router that allows you to create endpoints that send data back to your page, so you can populate the dropdown menu.

Some if/else statements in the do_GET should be sufficient.

I would prefer this as a starting point once you reach the limitations, and begin to learn how spaghetti gets made, then move into using flask, or something like cherrypy.

goodluck

Upvotes: 4

mVChr
mVChr

Reputation: 50195

WTForms should do the trick as a simple standalone form builder.

See the crash course for examples to create the form you describe.

Upvotes: 3

Related Questions