EOSC-Synergy

EGI SSO

VO university.eosc-eynergy.eu


Cel

Zadanie

Baza danych

Backend

Frontend

Technologie

Przykład

Baza danych

plantlst.txt
"Symbol","Synonym Symbol","Scientific Name with Author","Common Name","Family"
"ABAB","","Abutilon abutiloides (Jacq.) Garcke ex Hochr.","shrubby Indian mallow","Malvaceae"
"ABAB","ABAM5","Abutilon americanum (L.) Sweet","",""
...
insert.py
#! /usr/bin/env python
import csv
import pymongo

if __name__ == '__main__':
    columns = ["Symbol", "Synonym Symbol", "Scientific Name with Author", "Common Name", "Family"]
    entries = []
    with open('plantlst.txt') as infile:
        reader = csv.reader(infile)
        next(reader) # skip the first line
        for row in reader:
            entries.append(dict(zip(columns, row)))

    client = pymongo.MongoClient('mongodb://localhost:27017')
    db = client['db']
    plants = db['plants']
    plants.insert_many(entries)
Sprawdzenie czy działa jak należy
$ mongo
> use db
> db.plants.count()
90909

Backend

backend.py
#! /usr/bin/env python
import pymongo

from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class Plants(Resource):
    def __init__(self):
        self.client = pymongo.MongoClient('mongodb://localhost:27017')
        self.db = self.client['db']
        self.plants = self.db['plants']
        self.columns = ["Symbol", "Synonym Symbol", "Scientific Name with Author", "Common Name", "Family"]

    def get(self):
        results = self.plants.find({}, {'_id': 0})
        results = list(map(dict, results))
        return results

api.add_resource(Plants, '/')

if __name__ == '__main__':
    app.run(debug=True, port=5000)
Sprawdzenie czy działa jak należy
$ curl -s http://localhost:5000 | jq '. | length'
90909

Frontend

frontend.py
#! /usr/bin/env python
from flask import Flask, render_template
import requests

app = Flask(__name__, template_folder='templates')


@app.route('/')
def index():
    r = requests.get('http://localhost:5000')
    plants = r.json()
    return render_template('index.html', plants=plants[:100])


if __name__ == '__main__':
    app.run(debug=True, port=8080)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<table class="table table-striped">
    <tr>
        <th>Symbol</th>
        <th>Synonym Symbol</th>
        <th>Scientific Name with Author</th>
        <th>Common Name</th>
        <th>Family</th>
    </tr>
    {% for plant in plants %}
    <tr>
        <td>{{ plant['Symbol'] }}</td>
        <td>{{ plant['Synonym Symbol'] }}</td>
        <td>{{ plant['Scientific Name with Author'] }}</td>
        <td>{{ plant['Common Name'] }}</td>
        <td>{{ plant['Family'] }}</td>
    </tr>
    {% endfor %}
</table>
</body>
</html>
Sprawdzenie czy działa jak należy

Komentarze