Trigger python code from Browser button click?Answer below


Below is a basic way tried and tested with flask application thought to share.

Considering flask is installed
Create directories
mkdir /home/pi/flask/
mkdir /home/pi/flask/static/
mkdir /home/pi/flask/template/ #this will start application can locate under flask dir (sudo python &)

#!/usr/bin/env python
from flask import Flask, flash, redirect, render_template, request, session, abort
import os
import time
import subprocess
import sys
import os
import argparse
import glob

app = Flask(__name__)

def index():
    return render_template('index.html')

@app.route('/task/',methods = ['POST', 'GET'])
def table1():
        print("Task started")
      '/home/pi/flask/')  #this call the os script 
                return render_template('index.html')

if __name__ == '__main__':, host='')

/home/pi/flask/ #will look like

#python code you want to execute
python /home/pi/flask/
#anything ,,,,,,,

And in template directory create index.html

<!DOCTYPE html>
<body style="background-color:powderblue;">
<form action="/task/" method="post">
    <button style="height:150px;width:150px">Task</button>

start the
That all go to browser and with click of button control gopigo

Ip address on gopigo:5000


1 Like


Some questions from a “Dexter Industries Raspian For Robots” and GoPiGo3 perspective:

  1. How does Flask differ from the Apache2 server already installed on R4R?
  2. Does Flask coexist well with the R4R Apache2 and supported R4R “home page” (with noVNC / console buttons)


  1. Would it be possible, more “GoPiGo-ish”, to add user task button/application mapping to a “custom button div” on the R4R “home page” and not need Flask?
  2. If so, perhaps DI would add a “user button div” and a “user output div” on the R4R “home page” and publish an example how to publish the user button, connect the user button to a user application, and how the app can publish to the “user output div”.
  3. Would dynamic HTML be possible in the “user output div” area with the R4R base installation?
  4. Does DI have a preferred user web interface / programming method for R4R?


Is this the way to install it? (“install flask on Apache” instructions at )



Below website is reference

I noticed the flask was already available on gopigo.
This is a customize way to control python script from browser also remotely.
Currently I am controlling from my ipad.

Was able to access from local system via chrome browser



  1. Theoretically yes, that’s possible.

  2. I could probably see this working if it were a dedicated web interface for it, but in our case dex.local is only meant for giving you the means to connect to the Raspberry Pi.

  3. I’m not exactly sure as I don’t do much HTML.

  4. We prefer offering the visual user interface (web) to the user on DexterOS and on R4R, we try to steer him to using the terminal as much as possible. The only thing that remains visual are the desktop apps for the line follower or the GoPiGo or for Scratch.

Long story short, I think adding this kind of button that can trigger a task to execute adds too much complexity for nothing when it’s already simple to create say a service specifically tailored to the user’s needs that exposes something to some port (or that reads the input from a button continuously). I like keeping things simple and I’m trying to avoid complexity as that in the long term can only mess things.

Thank you!