0

Here's an overview of what I'm trying to do:

  • I'm reading data from a GSM modem and I am storing this in a database.

  • I'm doing this using a Python script and a MySQL database.

  • So here Python is just a back end, I want to create a front end using an HTML file. This would go up on a server later. (There is a particular 'look' I want to create for the HTML file.)

So do I first create the HTML file and then link it with Python? Do note that the Python script populates the database with live data.

Or do I create this HTML file in Python itself? Will I be able to achieve the feel I want for the webpage.

Also I shall have some options in the HTML page. When one of these options are selected, the Python script needs to be able to react to it and send out a message on the GSM modem. So can I get HTML to start running a Python script?

What do I do to achieve the above?

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
Anon
  • 845
  • 5
  • 30
  • 50

1 Answers1

3

What you basically need is MVC (Model View Controller), whose different components would be:

  1. Model: Your MySQL database containing the data
  2. View: Your HTML front end
  3. Controller : Where your main logic would reside, like what command to send to Modem when something on UI is clicked, or what data to pull from database and plot in the UI

Web Frameworks are the way to go. For a very simple project (you wanna finish it in less than an hour), web.py, flask and bottle would be good. For something more elaborate you may want to go with Django.

Each of these allow you to create your own HTML files and communicate with the backend using some templating scheme and control logic.

The way i usually communicate between the frontend and the backend is using AJAX queries. The advantage of this approach is that tomorrow if you decide to change the underlying framework, your UI code remains the same.

Let us now take one sample task: The user selects a particular command to send to the GSM Modem and waits for the response, if it were successful or not.

  1. On the UI you can send an AJAX POST or GET Request containing information about the command. Let us assume it is something like "AT", so you decide to POST it to a URL you have defined in your Routes information, let it be /execute
  2. Now the function corresponding to that Route entry gets executed, which will retrieve the parameter, being "AT", will make the necessary packet understood by the Modem and send it and get some response from Modem
  3. The function RETURNS with this response, and the control goes back to the UI
  4. In the UI you can have code to show the status of the task you wanted to execute

You can do similarly for pulling data from Database.

Nipun Batra
  • 11,007
  • 11
  • 52
  • 77