Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Building a graphical Business data Analysis platform based on flask Framework and pygal+sqlit3

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

one。 Preface

First, let's talk about the main framework and the characteristics of the main graphics library: (personal opinion)

A heavyweight web framework developed by Django:python, which integrates MVC and ORM technologies, is designed to make it easy to develop complex, database-driven websites. However, due to various reasons, few enterprises use it to develop large websites, but a lot of them are used for operation and maintenance. It focuses on the reusability and "pluggability" of components.

Flask:python developed a lightweight web framework, it uses a simple core, with extension to add other functions, the flexibility of expansion is very good, and easy to get started.

Echars: Baidu open source image library, the interface can be very cool, but mainly based on JS, Django, flask support for it is not very good, especially the background using python to process data to the foreground is very complex.

Highchars: a chart library written in pure JavaScript, with good compatibility and cross-platform. However, the data needs to be converted to json format before it can be displayed, which is also a big burden for the data that needs to be processed by python in the background. The support of Django and Flask is not very good.

Pygal: a dynamic SVG chart library developed by Python, which may not be as powerful as Echars or Highchars, but the advantage is that it is a library developed by python, using the native syntax of python, which is easy to use and does not require much complex conversion. And flask's support for it is very good, relatively speaking, Django's support for pygal is almost.

The database chooses sqlite3, a lightweight relational database. Because the data are all statistical data, the amount of data is small, and there is no need for particularly complex operations.

To sum up, after various attempts, the architecture of Flask+pygal+sqlite3 is determined.

two。 Environmental preparation

1. Install Python

1)。 Python version:

3.4.3: note that you choose to add python to the environment variable during installation

2)。 Description

It is recommended to choose a higher version of Python, because the latter two libraries need to be installed using the pip tool that comes with python, otherwise it is easy to install the installation package if you download the library directly, mainly due to the corresponding problems of the version and the lack of dependent libraries.

two。 Install the Flask framework

Execute in command line mode:

Pip install Flask

3. Install the graphics library

Execute in command line mode:

Pip install pygal

4. Install the ORM mapping library

Execute in command line mode:

Pip install flask-sqlalchemy

5. Download sqlite3 database

Download directly from the official website: http://www.sqlite.org/download.html

Select: sqlite-tools-win32-x86-3170000.zip

Contains three executable files after decompression

Third, the process of platform building

1. Create a database

1) in command line mode, change to the sqlite3 directory and execute the following command:

Sqlite3.exe dzj.db

2) then enter the sqlite3 command line mode:

Create table appinfo (id integer key autoincrement, year varchar (32), month varchar (32), cnt varchar (32)

3) Field description (primary key must be set)

Year year

Month month

Number of Cnt licen

4) insert data

two。 The process of building platform

1) create a project file directory (dzj)

2) create a static folder and a templates folder under the project file directory (dzj)

3) copy the created dzj.db database to the current directory (dzj directory)

4) add the dzj.py file (with the same name as the project) under the project file directory (dzj), and add the following code:

From flask import Flask Render_templateimport pygalfrom dbconnect import dbfrom models import Appinfoapp = Flask (_ _ name__) @ app.route ('/') def APPLYTBLINFO (): db.create_all () # when executed on the first call, you can appinfos = Appinfo.query.all () # # Select year list_year = [] # # Select month list_month = [] # number corresponding to month map_cnt = {} For info in appinfos: if info.year not in list_year: list_year.append (info.year) map_ [info.year] = [int (info.cnt)] else: map_ CNT [info.year] .append (int (info.cnt)) if info.month not in list_month: list_month.append (info.month) Line_chart = pygal.Line () line_chart.title = 'Information' line_chart.x_labels = map (str List_month) for year in list_year: line_chart.add (str (year) + "year", map_ CNT [year]) return render_template ('index.html', chart=line_chart) if _ _ name__ = =' _ main__': app.run (debug=True)

5) add the dbconnect.py file under the project file directory (dzj) as follows:

From flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport osapp = Flask (_ _ name__) dbpath = app.root_path.replace ("\" "/") # Note the direction of the slash app.config ['SQLALCHEMY_DATABASE_URI'] = r'sqlite:///'+dbpath+'/dzj.db'#app.config [' SQLALCHEMY_DATABASE_URI'] = r'sqlite:///D:/Python/dzj/dzj.db'app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = True#print (app.config [' SQLALCHEMY_DATABASE_URI']) db = SQLAlchemy (app)

6) add the models.py file under the project file directory (dzj) as follows:

Number of from dbconnect import db## license applications class Appinfo (db.Model): _ _ tablename__='appinfo' # # Note this sentence The primary key id = db.Column (db.Integer, primary_key=True) year = db.Column (db.String (20)) month = db.Column (db.String (20)) cnt = db.Column (db.String (20)) def _ init__ (self, year, month) must be set on some instances on the Internet. Cnt): self.year = year self.month = month self.cnt = cnt def _ str__ (self): return self.year+ ":" + self.month+ ":" + self.cnt def _ repr__ (self): return self.year+ ":" + self.month+ ":" + self.cnt def save (self): db.session.add (self) db.session.commit ()

7) add index.html under the templates file as follows:

Data trend chart analysis data chart overview quantity analysis chart

8) change to the directory where dzj is located at the command line, and execute:

Python dzj.py

No error is reported in the following figure, which means the operation is successful:

9) enter: http://127.0.0.1:5000/ in the browser to view the result

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report