Data is retrieved from a database system using the SQL language. Data is everywhere and software applications use that. Data is either in memory, files or databases.
Python数据库。
Python has bindings for many database systems including [MySQL](https://pythonspot.com/mysql-with-python), [Postregsql](https://pythonspot.com/python-database-postgresql/), Oracle, Microsoft SQL Server and Maria DB.
One of these database management systems (DBMS) is called SQLite. SQLite was created in the year 2000 and is one of the many management systems in the database zoo.
Python 具有许多数据库系统的绑定,包括 [MySQL](https://pythonspot.com/mysql-with-python), [Postregsql](https://pythonspot.com/python-database-postgresql/), Oracle, Microsoft SQL Server 和 Maria DB。
SQL is a special-purpose programming language designed for managing data held in a [databases](https://pythonspot.com/python-database/). The language has been around since 1986 and is worth learning. The [is an old funny video about SQL](https://www.youtube.com/watch?v=5ycx9hFGHog)
SQLite, a relational database management system. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain.
It is a self-contained, serverless, zero-configuration, transactional SQL database engine. The SQLite project is sponsored by Bloomberg and Mozilla.
它是一个自包含,无服务器,零配置的事务型 SQL 数据库引擎。SQLite 项目由彭博社和 Mozilla 赞助。
## 安装 SQLite:
Use this command to install SQLite:
使用此命令安装 SQLite:
```py
$sudoapt-getinstallsqlite
```
Verify if it is correctly installed. Copy this program and save it as test1.py
验证是否正确安装。 复制此程序并将其另存为`test1.py`
```py
#!/usr/bin/python
...
...
@@ -62,41 +62,41 @@ finally:
```
Execute with:
执行:
```py
$pythontest1.py
```
It should output:
它应该输出:
```py
SQLiteversion:3.8.2
```
## What did the script above do?
## 上面的脚本做了什么?
The script connected to a new database called test.db with this line:
该脚本使用以下代码行连接到名为`test.db`的新数据库:
```py
con=lite.connect('test.db')
```
It then queries the database management system with the command
然后,它使用以下命令查询数据库管理系统
```py
SELECTSQLITE_VERSION()
```
which in turn returned its version number. That line is known as an SQL query.
依次返回其版本号。 该行称为 SQL 查询。
## SQL 创建和插入
The script below will store data into a new database called user.db
下面的脚本会将数据存储到名为`user.db`的新数据库中
```py
#!/usr/bin/python
...
...
@@ -117,14 +117,14 @@ with con:
```
SQLite is a database management system that uses tables. These tables can have relations with other tables: it’s called relational database management system or RDBMS. The table defines the structure of the data and can hold the data. A database can hold many different tables. The table gets created using the command:
We can explore the database using two methods: the command line and a graphical interface.
我们可以使用两种方法浏览数据库:命令行和图形界面。
_ 从控制台:_ 要使用命令行进行浏览,请键入以下命令:
...
...
@@ -184,7 +184,7 @@ sqliteman
## SQL 数据库查询语言
SQL has many commands to interact with the [database](https://pythonspot.com/python-database/). You can try the commands below from the command line or from the GUI:
A framework may offer some or all of these features.
框架可以提供部分或全部这些功能。
例如,Flask Web 应用程序框架不支持数据库,您将需要一个单独的模块来使用数据库。 Django Web 应用程序框架默认支持数据库。
...
...
@@ -23,25 +23,27 @@ A framework may offer some or all of these features.
## 为什么要使用 Web 框架?
As you are doing web development, you want to avoid spending time on programming things that have already been solved. On the other hand, if you are an experienced web developer a web framework may not offer everything you need.
在进行 Web 开发时,您要避免花费时间在已经解决的事情上进行编程。 另一方面,如果您是经验丰富的 Web 开发人员,则 Web 框架可能无法提供您所需的一切。
## 存在哪些 Python Web 框架?
Django and Flask are the most popular web frameworks. However, you may want to evaluate the frameworks. An overview:
![python web development](img/5a539b08d07773dbba46f659b5d1743c.jpg)
在 Github 上提及框架的项目数量。
## Django
...
...
@@ -62,15 +64,15 @@ The most popular python web application framework is **Django**, followed by **F
* 国际化
* 安全
If you want to know more about Django, [read here.](https://pythonspot.com/django-tutorial-building-a-note-taking-app/)Did you know the websites of [NASA](https://www.nasa.gov/), [Bitbucket](https://bitbucket.org/) and [Pinterest](https://www.pinterest.com/) were made with Django?
[Flask](http://flask.pocoo.org/)is a Python micro framework which is modular by design. The framework is intended to build web apps. Flask does not have a specific database system or ORM system. If you want to use a database, you’ll have to use extensions. Flask is often combined with SQLAlchemy for database use.
Lets display random quotes instead of always the same quote. We will need to pass both the _name variable_ and the _quote variable_. To pass multiple variables to the function, we simply do this:
We will need to pick a random quote. To do so, we use this code:
我们将需要选择一个随机引文。 为此,我们使用以下代码:
```py
quotes=["'If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.' -- John Louis von Neumann ",
...
...
@@ -210,7 +209,7 @@ quote = quotes[randomNumber]
```
The first thing you see is we have defined an array of multiples quotes. These can be accessed as quote[0], quote[1], quote[2] and so on. The function **randint()** returns a random number between 0 and the total number of quotes, one is subtracted because we start counting from zero. Finally we set the quote variable to the quote the computer has chosen. Copy the code below to app.py:
While functional, the login screen looks like an early 90s User Interface (UI). We picked a random login template from [codepen.io](https://codepen.io). We create the directory /static/ with the file style.css.
As you may have seen, there is no logout button or functionality. Creating that is very easy. The solution proposed below is only one of the many solutions. We create a new route /logout which directs to the function logout(). This function clears the session variable and returns to the login screen.
If you want a multi-user login system, you should add a database layer to the application. Flask does not have out of the box database support. You have to use a third party library if you want database support. In this tutorial we will use SqlAlchemy. If you do not have that installed type:
We have demonstrated a simple login app above. However, it is your job to properly secure it. There are many guys out there that are going to try to break into your app.
To display awesome charts we first need some data. There are two common ways to get data in web apps: data from servers using an API (usually JSON) and data from databases. I use the Fixer.io JSON API to get some financial data, but any JSON API should do. If you are unfamiliar with [JSON, see this article.](https://pythonspot.com/json-encoding-and-decoding-with-python/)
要显示很棒的图表,我们首先需要一些数据。有两种常见的方法可以在 Web 应用程序中获取数据:使用 API(通常为 JSON)的服务器中的数据和数据库中的数据。 我使用 Fixer.io JSON API 来获取一些财务数据,但是任何 JSON API 都可以。 如果您不熟悉 JSON,[请参阅此文章](https://pythonspot.com/json-encoding-and-decoding-with-python/)。
我们编写此脚本来获取汇率:
...
...
@@ -33,7 +33,7 @@ print rates
## 使用 Flask 的 Google Charts API
With the [Google Charts API](https://developers.google.com/chart/interactive/docs/gallery) you can display live data on your site. There are a lot of great charts there that are easy to add to your Flask app. We simply give the data that we got from the server through JSON and parsed, to the Google Charts API.
在本教程中,您将学习如何使用 Flask 进行表单验证。 表单在所有 Web 应用程序中都扮演着重要角色。
...
...
@@ -12,7 +12,7 @@ Flask web form
## CSS 与 Flask
We use bootstrap to style the form.Bootstrap is a popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web. It makes front-end web development faster and easier. The output will be:
我们使用 Bootstrap 来设置表单的样式。Bootstrap 是流行的 HTML,CSS 和 JS 框架,用于在 Web 上开发响应式,移动优先项目。 它使前端 Web 开发更快,更轻松。 输出将是:
[**Flask**](/python-flask-tutorials) webapp with pie chart In this article you will learn how to create great looking charts using [Chart.js](http://www.chartjs.org) and [**Flask**](/python-flask-tutorials).
带有饼图的 Flask webapp
Chart.js is a javascript library to create simple and clean charts. All of them are HTML5 based, responsive, modular, interactive and there are in total 6 charts.
@@ -48,7 +47,7 @@ Chart.js is a javascript library to create simple and clean charts. All of them
```
Create the directory /static/ and add the file [Chart.min.js](https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js) to it. You can get it either from the Chart.js website or use the link. Finally go into the home directory and create app.py with this contents:
Open [http://127.0.0.1:5001/](http://127.0.0.1:5001/) and you will see the array values[] plotted with the data in labels[].
We simply pass these two arrays to render_template(). This means that most of the magic occurs in the template. Chart.js is a client-side javascript library which is why our app.py is very minimal.
To create a line chart, we can simply modify the chart.html template. Change it to:
要创建折线图,我们只需修改`chart.html`模板即可。 更改为:
```py
<metacharset="utf-8">
...
...
@@ -147,7 +149,7 @@ To create a line chart, we can simply modify the chart.html template. Change it
### 创建饼图
To create a pie chart, we must modify the application code slightly. We need 3 arrays: values, labels and colors. Colors are defined in hexadecimal, as usual in HTML. To iterate them in one loop, we zip them.
要创建饼图,我们必须稍加修改应用程序代码。 我们需要 3 个数组:值,标签和颜色。 与 HTML 一样,颜色以十六进制定义。 要在一个循环中对其进行迭代,请压缩它们。