1

i just would like to run a python script by clicking on a html button. python script is reallly simple. when you run it, it just add a "name" and "age" data in my database.

import sqlite3

conn = sqlite3.connect('bdaremplir.sqlite')
cur = conn.cursor()

"""cur.execute('DROP TABLE IF EXISTS latable')
cur.execute('CREATE TABLE latable (noms TEXT, age INTEGER)')"""



nom = 'TheName'
age = 100

cur.execute('''INSERT OR IGNORE INTO latable (noms, age) VALUES ( ?, ? )''', (nom, age))
conn.commit()

and the basics of html

<!DOCTYPE html>
<html>
    <head>
        <title>visualisateur</title>
    </head>
    <body>
        <button></button>
    </body>
</html>

now from here i don't at all what i can do. if anyone can help... thank you.

  • 1
    You'll need to either compile your Python using Emscripten, or use a JavaScript interpreter for python, or send a request to the server and run python server-side. However, keep in mind that since the database exists server-side and not client side, you will *not* be able to run that code client-side. – Michael Bianconi Aug 16 '19 at 17:04
  • Unless I'm missing something about the system (it seems like you just have these two pieces of code), there are several other components you need. I think starting with a simple web framework like Flask might be the best move. – it's-yer-boy-chet Aug 16 '19 at 17:09
  • To run Python code on the server you’ll need to learn a framework like Django, Flask or Bottle. I have only used Bottle - it’s easy and lightweight and I highly recommend it. – Chris Aug 16 '19 at 17:16

1 Answers1

2

Web browsers only know how to run JavaScript, not Python, so unless you really want to run Python in the browser1, you have to learn about the client-server architecture of the web.

Basically, you have to

  1. make your Python program available as a web server, and
  2. send a request to that server when your HTML button is clicked.

Running a web server

There are many ways to implement a web server in Python. When you get to writing a proper application, you'll probably want to use a library like Flask, Django, or others. But to get started quickly, you can use the built-in Python HTTP server with CGI:

  1. Create a Python script named handle_request.py with the following content:

     #!/usr/bin/env python3
     print("Content-Type: text/plain\n")
     print("hello world")
    

and put it into the cgi-bin directory next to your HTML file and make sure you can run it by typing "handle_request.py" into the console; 2. Run the built-in HTTP server:

    python3 -m http.server --bind localhost --cgi 8000
  1. Open http://localhost:8000 in your browser to access the server.

You should see the listing of the directory, including your HTML file and the cgi-bin directory. Clicking the HTML file should display it in the browser (with a URL like http://localhost:8000/test.html), and opening http://localhost:8000/cgi-bin/handle_request.py will run the script we've created and display its response as a web page. You can add your code to the script, and it will run whenever the browser accesses its URL.

Making a request from your web page

Now the question is how to make the browser access the http://localhost:8000/cgi-bin/handle_request.py URL when a button on your page is clicked.

To do that you need to invoke the API for making requests from JavaScript. There are different ways to do that (e.g. XMLHttpRequest or jQuery.ajax()), but a simple way that works in the modern web browsers is fetch():

<button id="mybutton"></button>

<script>
document.getElementById("mybutton").onclick = async function() {
  let response = await fetch("http://localhost:8000/cgi-bin/handle_request.py");
  let text = await response.text();
  alert(text);
}
</script>

Now when you click the button, the browser will make a request to the specified URL (thus running your script), but instead of displaying the results as a web page in a tab, it will be made available to your JavaScript.

notes

1 ...which you probably don't at this point, but if you do, see the pointers by Michael Bianconi.

Nickolay
  • 31,095
  • 13
  • 107
  • 185