mirror of
https://github.com/google/pybadges.git
synced 2026-01-07 03:40:09 +01:00
Merge pull request #23 from google/docs
Add a more complete server example
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
42
server-example/templates/index.html
Normal file
42
server-example/templates/index.html
Normal file
@@ -0,0 +1,42 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>pybadges demo</title>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap" rel="stylesheet">
|
||||
<style>
|
||||
table {
|
||||
font-family: 'Roboto Mono', monospace;
|
||||
border-spacing: 1em;
|
||||
text-align: left;
|
||||
border-collapse: separate;
|
||||
}
|
||||
|
||||
img {
|
||||
padding-left: 5em;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<table>
|
||||
<tbody>
|
||||
{% for badge in badges %}
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
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" }})
|
||||
|
||||
</pre>
|
||||
</td>
|
||||
<td><img src={{ badge.url }}></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user