์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

CSV ํŒŒ์ผ ์ฝ์–ด๋“ค์—ฌ์„œ highcharts + Flask ๊ทธ๋ž˜ํ”„ ๊ตฌํ˜„ํ•˜๊ธฐ.

  • -

 

 

https://www.highcharts.com/

 

Interactive JavaScript charts for your webpage | Highcharts

"I absolutely LOVE Highcharts & maps, very cool! We use it for a web metrics dashboard, which is shared with internal marketing stakeholders. The tool is brilliant and the API documentation is super-helpful. I set up some basic, manual reports using Highch

www.highcharts.com

Flask์— ์‹ค์‹œ๊ฐ„ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ์‹ถ์–ด์„œ, Highcahrts๋ผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 

*ํŒŒ์ผ ๊ตฌ์„ฑ

app.py (Flask run server ํŒŒ์ผ) 

csv-to-sqlite.py (csv๋ฅผ sqlite DB์— ์ €์žฅํ• )

db.sqlite(DBํŒŒ์ผ)

data.csv(์‹œ๊ฐํ™”ํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ)

graph.html(htmlํŒŒ์ผ)

static ํŒŒ์ผ๋“ค(๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•ด์ค„ JS ํŒŒ์ผ)

 



* CSV ํŒŒ์ผ ์ž‘์„ฑํ•˜๊ธฐ.

์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. [timestamp , value] ๋‘๊ฐ€์ง€ ์—ด(column)๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. 

1388534400,5
1390040000,3
1390043600,9
1390047000,1
1390050000,3
1390055000,5

*CSV ๋ฐ์ดํ„ฐ sqlite DB์— ์ €์žฅํ•˜๊ธฐ.

import csv, sqlite3
conn = sqlite3.connect("db_6.sqlite")   # ์ €์žฅํ•  DBํŒŒ์ผ ์ด๋ฆ„
curs = conn.cursor()

curs.execute("CREATE TABLE measures (timestamp INTEGER, measure INTEGER)")  
#TABLE : measures , ์ปฌ๋Ÿผ์ด๋ฆ„ : (timestamp , measure)

reader = csv.reader(open('data_1.csv', 'r'))   # CSVํŒŒ์ผ ์ฝ๊ธฐ๋ชจ๋“œ๋กœ ์—ด๊ธฐ
for row in reader:                             #for ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•˜์—ฌ DB์— ์ž‘์„ฑ
    to_db = [(row[0]), (row[1])]
    curs.execute("INSERT INTO measures (timestamp, measure) VALUES (?, ?);", to_db)
    
conn.commit()  #์ปค๋ฐ‹ (์Œ“์•„๋‘” ๋ช…๋ น ์‹คํ–‰)
conn.close()   

 

์ด์ œ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค ๋ฐ์ดํ„ฐ๋ฅผ SQLite์— ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ๋˜์—ˆ๋Š”์ง€ DBํŒŒ์ผ์„ ํ•œ๋ฒˆ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž˜ ๋“ค์–ด๊ฐ”์Šต๋‹ˆ๋‹ค.

 

*HTMLํŒŒ์ผ

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Highstock Example</title>
        <script src="{{ url_for('static', filename='jquery-1.8.3.min.js') }}"></script>
		<script type="text/javascript">
		$(function () {
    $.getJSON('http://127.0.0.1:5000/data.json', function (data) {
        // Create the chart
        $('#container').highcharts('StockChart', {
            rangeSelector : {
                selected : 1
            },
            title : {
                text : 'My Sensor'
            },
            series : [{
                name : 'Value',
                data : data,
                tooltip: {
                    valueDecimals: 2
                }
            }]
        });
    });
});
		</script>
	</head>
	<body>
<script src="{{ url_for('static', filename='highstock.js') }}"></script>
<script src="{{ url_for('static', filename='highcharts-more.js') }}"></script>
<script src="{{ url_for('static', filename='exporting.js') }}"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
 
	</body>
</html>

*app.py ํŒŒ์ผ

from flask import Flask, render_template, request
import sqlite3
import json
 
app = Flask(__name__)
 
 
@app.route("/data.json")
def data():
    connection = sqlite3.connect("db_6.sqlite")
    cursor = connection.cursor()
    cursor.execute("SELECT 1000*timestamp, measure from measures")
    results = cursor.fetchall()
    
    return json.dumps(results)
 
@app.route("/graph")
def graph():
    return render_template('graph.html')
 
 
if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=5000)

*Static ํŒŒ์ผ๋“ค

 

static.zip
0.12MB

https://www.fontenay-ronan.fr/dynamic-charts-with-highcharts-sqlite-and-python/

 

Dynamic charts with Highcharts, SQLite and Python – Ronan's blog

I used to use a Raspberry Pi as a data logger for many sensors, here is how I create a nice front-end for data visualization. - Store data : SQLite - Serve content : Python with Flask - Plot data : Hightcharts / Highstock 1 Database 1.1 Create database A q

www.fontenay-ronan.fr

 

(์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.)

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.