Paso 9: Todo el código
Plantilla html de Jinja2:
Interface.html
<! doctype html >< cabeza >< title > RPi Radio < / título >< / cabeza >< cuerpo > {% si nombre %} < h1 > bienvenida, {{nombre}} < / h1 > {% más %} < h1 > Bienvenido a RPi Radio < / h1 > {% endif %} < método del formulario = 'POST' Acción = '/' >< tipo de entrada = 'Enviar' nombre = 'Enviar' valor = "activar radio" / >< tipo de entrada = 'Enviar' nombre = 'Enviar' valor = "apagar la radio" / >< br / > <!doctype html> <head> <title>RPi Radio</title> </head> <body> {% if name %} <h1>Welcome, {{ name }}</h1> {% else %} <h1>Welcome to RPi Radio</h1> {% endif %} <form method='POST' action='/'> <input type="submit" name="submit" value="turn radio on" /> <input type="submit" name="submit" value="turn radio off" /> <br/> <select name='station'>{{ stations|safe }}</select> <input type="submit" name="submit" value="change" /> <br/> <input type="submit" name="submit" value="update playlist" /> <br/> <hr/> <input type="submit" name="submit" value="-5" /> {{ volume }} <input type="submit" name="submit" value="+5" /> <hr/> </form> </body> < br / > import subprocess host = '0.0.0.0' port = 1234 txtFile = 'stations.txt' templateFile = 'interface.html' def isInteger(s): try: int(s) return True except ValueError: return False def mpcCommand(cmd): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) return p.stdout.read()
Archivos sueltos de Python:
predefines.py
subproceso de importación #!/usr/bin/python # -*- coding: utf-8 -*- from flask import Flask from flask import render_template from flask import request from predefines import host, port, txtFile, templateFile from predefines import isInteger, mpcCommand import subprocess app = Flask(__name__) methods=['GET', 'POST']) def hello_world(name='Flask FM'): stations = [] stationURLs = [] stationOutput = '' if request.method == 'POST': if request.form['submit'] == 'turn radio on': mpcCommand(['mpc', 'play']) elif request.form['submit'] == 'turn radio off': mpcCommand(['mpc', 'stop']) elif request.form['submit'] == 'change': mpcCommand(['mpc', 'play', str(request.form['station'])]) elif request.form['submit'] == '+5': mpcCommand(['mpc', 'volume', '+5']) elif request.form['submit'] == '-5': mpcCommand(['mpc', 'volume', '-5']) elif request.form['submit'] == 'update playlist': mpcCommand(['mpc', 'clear']) for stationURL in stationURLs: mpcCommand(['mpc', 'add', stationURL]) cmd=['mpc', '-f', '%position%'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) position = p.stdout.read() idx = position.split('[') position = idx[0].strip() if isInteger(position) == False: position = 0 x = 1 for station in stations: stationOutput += '<option value="' + str(x) + '" ' if x == int(position): stationOutput += 'selected="selected"' stationOutput += '>' + station + '</option>' x += 1 volume = mpcCommand(['mpc', 'volume']) return render_template(templateFile, name=name, stations=stationOutput.strip(), volume=volume) if __name__ == '__main__': app.run(host=host, port=port, debug=True)
Código del frasco de Python:
radio.py
de importación frasco frasco del frasco render_template de importación de importación de matraz solicite predefine importación host, puerto, txt, templateFile de predefine importación isInteger, subproceso de la aplicación de la importación de mpcCommand = métodos Flask(__name__) = ['GET', 'POST']) def hola_mundo (nombre = 'Frasco FM'): estaciones = [] stationURLs = [] stationOutput = '' si request.method == 'POST':mpcCommand (['mpc', 'play']) elif request.form['submit'] == 'Apague radio': mpcCommand (['mpc', 'stop'])Elif request.form['submit'] == '+ 5': mpcCommand (['mpc', 'volumen', ' + 5 ']) elif request.form['submit'] == '-5': mpcCommand (['mpc', 'volumen', ' -5 '])