Stap 9: Alle code
Jinja2 HTML-sjabloon:
interface.html
<! doctype html >< hoofd >< titel > RPi Radio < / title >< / head >< lichaam > {% als naam %} < h1 > Welkom, {{naam}} < / h1 > {% anders %} < h1 > Welkom bij RPi Radio < / h1 > {% endif %} < methode = 'POST' actie = '/' >< input type = "submit" naam = "submit" waarde = 'Schakel radio' / >< input type = "submit" naam = "submit" waarde = "uitschakelen 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()
Python losse bestand:
predefines.py
een subproces voor importeren #!/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)
Maatkolf van python code:
Radio.py
van kolf importeren kolf van kolf import render_template uit kolf importeren aanvragen bij predefines import host, poort, txtFile templateFile van predefines invoer isInteger, mpcCommand import subprocess app = Flask(__name__) methoden = ['krijgen', 'POST']) def hello_world (naam = 'Kolf FM'): stations = [] stationURLs = [] stationOutput = '' als request.method == 'POST':mpcCommand (['mpc', 'play']) elif request.form['submit "] == 'uitschakelen radio': mpcCommand (['mpc', 'stop'])Elif request.form['submit "] == '+ 5': mpcCommand (['mpc', 'volume', ' + 5 ']) elif request.form['submit"] == '-5': mpcCommand (['mpc', 'volume', ' -5 '])