python echarts mysql_Django,Pyecharts,MySql(MariaDB)图表展现

好久没有写blog了,今天奉上一篇原创

一、开发环境

Manjaro(4.14核心),Pycharm 18.1,miniconda(Python3.6.5),MariaDB10.2,pyecharts 0.4.1 ,django 2.0.2,pymysql

二、开发过程

1、pycharm新建一个django工程,取名为echart_demo;

2、在pycharm中打开终端,运行python manage.py startapp mycharts

3、项目的目录结构如下:

4、在mycharts目录中添加一个urls.py,内容如下:from django.urls import path

from . import views

urlpatterns = [

path('', views.index, name='index'),

]

5、编辑  echart_demo/settings.py,内容如下:# Application definition

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'mycharts',      #此处为新添加内容

]

6、继续编辑  echart_demo/settings.py 找到database配置段内容,修改如下:

此部分内容根据实际情况修改# Database

# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql',

'NAME': 'testdb',

'HOST':'192.168.1.9',

'PORT':'3306',

'USER':'user',

'PASSWORD':'pwd',

}

}

7、编辑  echart_demo/urls.py,内容如下:from django.contrib import admin

from django.urls import path,include

urlpatterns = [

path('admin/', admin.site.urls),

path('mycharts/', include("mycharts.urls")),     # 添加mycharts的urls

]

8、编辑 mycharts/views.py 内容如下:from django.db import connection

from django.http import HttpResponse

from django.template import loader

from pyecharts import Bar

REMOTE_HOST = "https://pyecharts.github.io/assets/js"

def exc_sql(sql):

cursor = connection.cursor()

cursor.execute(sql)

result = cursor.fetchall()

return result

def index(request):

template = loader.get_template('mycharts/pyecharts.html')

b=bar()

context = dict(

myechart=b.render_embed(),

host=REMOTE_HOST,

script_list=b.get_js_dependencies()

)

return HttpResponse(template.render(context, request))

def bar():

#_data = []

query_sql = "select  nation_name,nation_quant from Nation_Demo"

data_list = exc_sql(query_sql)

x=[i[0] for i in data_list]

y=[i[1] for i in data_list]

#_data.append()

bar=Bar("各国GDP柱状图",width=1000,height=700)

bar.add("各国GDP量",x, y, type="effectScatter", border_color="#ffffff", symbol_size=2,

is_label_show=True, label_text_color="#0000FF", label_pos="inside", symbol_color="yellow",

bar_normal_color="#006edd", bar_emphasis_color="#0000ff")

return bar

9、在mycharts目录下新建目录  templates,在刚建好的templates下再建立一个目录 mycharts,在这个新建的mycharts目录中添加一个pyecharts.html,编辑内容如下:html>

Proudly presented by PycCharts

{% for jsfile_name in script_list %}

{% endfor %}

{{ myechart|safe }}

10、最后的目录结构如下图:

三、数据库准备

create table Nation_Demo (

id           int(8) not null primary key auto_increment,

nation_name  nvarchar(20),

nation_quant int

)insert into Nation_Demo values (null,'中国',1500);

insert into Nation_Demo values (null,'美国',2500);

insert into Nation_Demo values (null,'英国',1200);

insert into Nation_Demo values (null,'俄罗斯',900);

insert into Nation_Demo values (null,'日本',2100);

insert into Nation_Demo values (null,'越南',500);

四、运行时错误与解决方案(如果没有报错,就不用理会下面内容)

运行时会报无MySqlDB的错误,由于Python3已经不再使用MySqlDB,需要用pymysql替代

1、编辑echart_demo/__init__.py 添加以下内容:import pymysql

pymysql.install_as_MySQLdb()

2、在终端中运行以下两条命令:python manage.py makemigrations

python manage.py migrate

五、更多的chart类型,请参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值