diff --git a/server-example/README.md b/server-example/README.md index d8dc3db..3adab1a 100644 --- a/server-example/README.md +++ b/server-example/README.md @@ -1,6 +1,7 @@ ## Simple Flask Server -This is a simple example of how badges can be served from a web server. +This is a simple example of how a +[Flask](https://flask.palletsprojects.com/) server can serve badges. ### Installing diff --git a/server-example/app.py b/server-example/app.py index a6501c4..cc7cb5e 100644 --- a/server-example/app.py +++ b/server-example/app.py @@ -11,20 +11,57 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -""" Example CI server that serves badges.""" +""" Example Flask server that serves badges.""" -from flask import Flask -import pybadges import flask +import pybadges -app = Flask(__name__) +app = flask.Flask(__name__) @app.route('/') -def serveBadges(): - badge = pybadges.badge(left_text='build', - right_text='passing', - right_color='#008000') +@app.route('/index') +def index(): + """Serve an HTML page containing badge images.""" + badges = [ + { + 'left_text': 'Build', + 'right_text': 'passing', + 'left_color': '#555', + 'right_color': '#008000' + }, + { + 'left_text': 'Build', + 'right_text': 'fail', + 'left_color': '#555', + 'right_color': '#800000' + }, + { + "left_text": + "complete", + "right_text": + "example", + "left_color": + "green", + "right_color": + "yellow", + "logo": + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAD0lEQVQI12P4zwAD/xkYAA/+Af8iHnLUAAAAAElFTkSuQmCC" + }, + ] + for b in badges: + b['url'] = flask.url_for('.serve_badge', **b) + return flask.render_template('index.html', badges=badges) + + +@app.route('/img') +def serve_badge(): + """Serve a badge image based on the request query string.""" + badge = pybadges.badge(left_text=flask.request.args.get('left_text'), + right_text=flask.request.args.get('right_text'), + left_color=flask.request.args.get('left_color'), + right_color=flask.request.args.get('right_color'), + logo=flask.request.args.get('logo')) response = flask.make_response(badge) response.content_type = 'image/svg+xml' diff --git a/server-example/templates/index.html b/server-example/templates/index.html new file mode 100644 index 0000000..0c98551 --- /dev/null +++ b/server-example/templates/index.html @@ -0,0 +1,42 @@ + + + + pybadges demo + + + + + + + + {% for badge in badges %} + + + + + + {% endfor %} +
+
+pybadges(left_text="{{ badge.left_text }}",
+         right_text="{{ badge.right_text }}",
+         left_color="{{ badge.left_color }}",
+         right_color="{{ badge.right_color}}",
+         logo={{ '"' ~ badge.logo ~ '"' if badge.logo else "None" }})
+
+
+
+ + + \ No newline at end of file diff --git a/server-example/test_app.py b/server-example/test_app.py index 3e7fd21..41b9d34 100644 --- a/server-example/test_app.py +++ b/server-example/test_app.py @@ -26,6 +26,6 @@ def client(): def test_image(client): - rv = client.get("/") + rv = client.get("/img?left_text=build&right_text=passing") assert b'build' in rv.data assert b'passing' in rv.data