python可视化——django驾驶舱的实现(下)

书接上文,没看上篇的小伙伴最好看一下。


前言

上篇文章中我们完成了django项目的创建和基本显示,那么本篇文章就将实现数据库与django的连接,并且完成数据的导入。


一、创建数据库

如果之前按照上篇讲的创建好了,可以不用看这个部分。

第一步:


下载MySQL Workbench, 通过MySQL connections创建或编辑连接,初始就只有一个连接,是安装MySQL Server时配置的local连接;

 我当时没有设置密码,所以可以直接进入,希望各位没有忘记密码~

第二步:

建立数据库,我的数据库名称是temp202205231411

1888857cb90f79ee395e164d8a9a27ba.png

二、创建连接,建立数据库模型

1.创建django与数据库间的连接

配置__init__.py

import pymysql
pymysql.install_as_MySQLdb()

配置 setting.py,建立与数据库的联系:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',   # 数据库引擎
        'NAME': 'temp202205231411',  # 数据库名,先前创建的
        'USER': 'root',     # 用户名,可以自己创建用户
        # 'PASSWORD': '',  # 没设密码就空着
        'HOST': '127.0.0.1',  # mysql服务所在的主机ip
        'PORT': '3306',         # mysql服务端口
    }
}

配置models.py,创建数据库模型:

本次创建模型时,我直接按照驾驶舱界面需要来进行配置:

from django.db import models
# Create your models here.
class house_mount(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class high_price(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class chengqu_mount(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)


class jiaoqu_mount(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class pinglun_mount(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class chengqu_level(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class area_heat(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class jiaoqu_level(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)


class redu_house(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class redu_bieshu(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class title(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class comment(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

class policy(models.Model):
    text1=models.CharField(max_length=100)
    text2=models.CharField(max_length=100)

执行数据库迁移:在终端里,先后执行这两句

python manage.py makemigrations

python manage.py migrate

配置 view.py:

from django.shortcuts import render
from myWebApp007.models import *


# Create your views here.
def index(request):

    myList1 = []
    myList2 = []
    allRows1 = house_mount.objects.all()
    for eachRow in allRows1:
        myList1.append(eachRow.text1)
        myList2.append(eachRow.text2)

    myList3 = []
    myList4 = []
    allRows2 = high_price.objects.all()
    for eachRow in allRows2:
        myList3.append(eachRow.text1)
        myList4.append(eachRow.text2)

    myList5 = []
    myList6 = []
    allRows3 = chengqu_mount.objects.all()
    for eachRow in allRows3:
        myList5.append(eachRow.text1)
        myList6.append(eachRow.text2)



    myList7 = []
    myList8 = []
    allRows4 = jiaoqu_mount.objects.all()
    for eachRow in allRows4:
        myList7.append(eachRow.text1)
        myList8.append(eachRow.text2)

    myList9 = []
    myList10 = []
    allRows5 = pinglun_mount.objects.all()
    for eachRow in allRows5:
        myList9.append(eachRow.text1)
        myList10.append(eachRow.text2)

    myList11 = []
    myList12 = []
    allRows6 = chengqu_level.objects.all()
    for eachRow in allRows6:
        myList11.append(eachRow.text1)
        myList12.append(eachRow.text2)

    myList13 = []
    myList14 = []
    allRows7 = area_heat.objects.all()
    for eachRow in allRows7:
        myList13.append(eachRow.text1)
        myList14.append(eachRow.text2)

    myList15 = []
    myList16 = []
    allRows8 = jiaoqu_level.objects.all()
    for eachRow in allRows8:
        myList15.append(eachRow.text1)
        myList16.append(eachRow.text2)

    myList17 = []
    myList18 = []
    allRows9 = redu_house.objects.all()
    for eachRow in allRows9:
        myList17.append(eachRow.text1)
        myList18.append(eachRow.text2)

    myList19 = []
    myList20 = []
    allRows10 = redu_bieshu.objects.all()
    for eachRow in allRows10:
        myList19.append(eachRow.text1)
        myList20.append(eachRow.text2)

    myList21 = []
    myList22 = []
    allRows11 = title.objects.all()
    for eachRow in allRows11:
        myList21.append(eachRow.text1)
        myList22.append(eachRow.text2)

    myList23 = []
    myList24 = []
    allRows12 = comment.objects.all()
    for eachRow in allRows12:
        myList23.append(eachRow.text1)
        myList24.append(eachRow.text2)

    myList25 = []
    myList26 = []
    allRows13 = policy.objects.all()
    for eachRow in allRows13:
        myList25.append(eachRow.text1)
        myList26.append(eachRow.text2)


    return render(request, 'index.html',
                  {"myList1": myList1, "myList2": myList2,"myList3": myList3, "myList4": myList4,
                   "myList5": myList5, "myList6": myList6,"myList7": myList7, "myList8": myList8,
                   "myList9": myList9, "myList10": myList10,"myList11": myList11, "myList12": myList12,
                   "myList13": myList13, "myList14": myList14,"myList15": myList15, "myList16": myList16,
                   "myList17": myList17, "myList18": myList18,"myList19": myList19, "myList20": myList20,
                   "myList21": myList21, "myList22": myList22,"myList23": myList23, "myList24": myList24,
                   "myList25": myList25, "myList26": myList26})

2. 向数据库中读入数据

2.1 数据处理

先把数据拿来,进行一波简单的处理;

数据链接:百度网盘 请输入提取码 
提取码:hdc3 

代码如下:

import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore")
df = pd.read_csv('北京小区数据信息.csv')

df=df[-df.所在区.isin(['非北京周边','海阳城区','宝坻','秦皇岛','永清','涞水','怀来','天津','霸州','大厂','廊坊','涿州','固安','崇礼'])]
df=df[-df.均价.isin(['价格待定元/㎡'])]
estate_single = df[df['均价'].str.contains('元/㎡')]
estate_single['均价'] = [int(i.split('元/㎡')[0]) for i in estate_single['均价']]
estate_mean = estate_single[['所在区', '均价']].groupby('所在区').mean()
estate_mean.reset_index(inplace=True)
estate_tao = df[df['均价'].str.contains('套')].reset_index(drop=True)

import re
strinfo = re.compile('万元|/套|起')
estate_tao['均价'] = estate_tao['均价'].apply(lambda x: strinfo.sub('',x))
estate_tao['均价']=estate_tao['均价'].astype(int)
estate_tao['均价']=estate_tao['均价'].sort_index()
#由于房产是以套为单位显示价格,所以只能结合实际,人工评估价格
for i in range(127):
    if estate_tao['小区名称'][i]=='恒大丽宫':
        estate_tao['均价'][i] = estate_tao['均价'][i]*5 #恒大丽宫面积极大
    elif estate_tao['均价'][i]>=1500:
        estate_tao['均价'][i] = estate_tao['均价'][i]*30 #例如圆明天颂户型较大,所以每平米更便宜一些
    elif estate_tao['均价'][i]<=1000 and estate_tao['所在区'][i]!='朝阳'and estate_tao['所在区'][i]!='海淀':
        estate_tao['均价'][i] = estate_tao['均价'][i]*100 #例如兴创荣墅
    elif estate_tao['均价'][i]<=1000 and estate_tao['所在区'][i]=='朝阳':
        estate_tao['均价'][i] = estate_tao['均价'][i]*130 #例如北京书院,户型较小,所以每平米更贵
    elif estate_tao['均价'][i]<=1000 and estate_tao['所在区'][i]=='海淀':
        estate_tao['均价'][i] = estate_tao['均价'][i]*130
    elif estate_tao['均价'][i]>1000 and estate_tao['均价'][i]<1500:
        estate_tao['均价'][i] = estate_tao['均价'][i]*55 #例如玖瀛府,户型较大,所以每平米更贵
    else:
        estate_tao['均价'][i] = estate_tao['均价'][i]*75
estate_tao = estate_tao.drop_duplicates()
estate = pd.concat([estate_single,estate_tao], axis=0)
estate = estate.drop_duplicates()
from sklearn import preprocessing
import  pandas
a=pd.DataFrame(estate['评论数'])
min_max_normalizer=preprocessing.MinMaxScaler(feature_range=(0,1))
#feature_range设置最大最小变换值,默认(0,1)
scaled_data=min_max_normalizer.fit_transform(a)
#将数据缩放(映射)到设置固定区间
price_frame_normalized=pandas.DataFrame(scaled_data)
#将变换后的数据转换为dataframe对象
print(price_frame_normalized)
estate['标准化热度'] = price_frame_normalized

import copy
estate1=copy.deepcopy(estate)
estate1['评论数']=estate1['评论数']*100
wnc = list(estate1.groupby(['均价', '评论数']).groups.keys())
estate1['评论数']=estate1['评论数']/100

bins = [0, 5000, 10000, 50000, 100000, 150000, 1000000]
group_names = ['低的档','中低档', '中档', '中高档', '高的档','超高档']
estate1['小区级别'] = pd.cut(estate1['均价'], bins, labels=group_names)
estate1['超高档'] = estate1['小区级别'].apply(lambda x : 1 if '超高档' in x else 0)
estate1['高的档'] = estate1['小区级别'].apply(lambda x : 1 if '高的档' in x else 0)
estate1['中高档'] = estate1['小区级别'].apply(lambda x : 1 if '中高档' in x else 0)
estate1['中档'] = estate1['小区级别'].apply(lambda x : 1 if '中档' in x else 0)
estate1['中低档'] = estate1['小区级别'].apply(lambda x : 1 if '中低档' in x else 0)
estate1['低的档'] = estate1['小区级别'].apply(lambda x : 1 if '低的档' in x else 0)

bins = [0, 100, 500, 1000, 2000] #设置10000为最大值
group_names = ['冷门', '一般', '热门', '非常热门']
estate1['热度级别'] = pd.cut(estate1['评论数'], bins, labels=group_names)
estate1['非常热门'] = estate1['热度级别'].apply(lambda x : 1 if '非常热门' in x else 0)
estate1['热门'] = estate1['热度级别'].apply(lambda x : 1 if '热门' in x else 0)
estate1['一般'] = estate1['热度级别'].apply(lambda x : 1 if '一般' in x else 0)
estate1['冷门'] = estate1['热度级别'].apply(lambda x : 1 if '冷门' in x else 0)

2.2 数据处理

建立与数据库的连接: 

import pymysql
conn = pymysql.connect(host='127.0.0.1',user='root',password='',database='temp202205231411')
cursor=conn.cursor()

将档次等信息输入数据库中的对应表格:

我使用的django驾驶舱共15个区域,我分别用13个表格的信息进行填入。

1.将楼盘档次数据导入:

bins = [0,10000, 30000, 100000, 150000, 1000000]
group_names = ['低的档','中低档', '中档', '中高档', '高的档']
estate1['小区级别'] = pd.cut(estate1['均价'], bins, labels=group_names)
estate1['高的档'] = estate1['小区级别'].apply(lambda x : 1 if '高的档' in x else 0)
estate1['中高档'] = estate1['小区级别'].apply(lambda x : 1 if '中高档' in x else 0)
estate1['中档'] = estate1['小区级别'].apply(lambda x : 1 if '中档' in x else 0)
estate1['中低档'] = estate1['小区级别'].apply(lambda x : 1 if '中低档' in x else 0)
estate1['低的档'] = estate1['小区级别'].apply(lambda x : 1 if '低的档' in x else 0)
qu_huxing = estate1[['所在区','低的档','中低档', '中档', '中高档', '高的档']].groupby('所在区').sum()
chengqu_dangci=['低档','中低档', '中档', '中高档', '高档']
mount_dangci=[51,120,255,24,5]
text1=chengqu_dangci
text2=mount_dangci
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_chengqu_level(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

2.将区域房价档次数据导入:

labels=['核心城区','中心城区','近郊区','远郊区','燕郊','北京周边']
#核心城区:东西城
#中心城区:朝阳、海淀、丰台、石景山
#近郊区:顺义、大兴、昌平、门头沟。通州区
#远郊区:平谷、怀柔、密云、延庆、房山
#北京周边:北京周边加香河
dangci=['低档', '中档', '高档','低档', '中档', '高档','低档', '中档', '高档','低档', '中档', '高档','低档', '中档', '高档','低档', '中档', '高档']

mount_dangci2=[0,0,5, 0,77,26, 3,174,3, 1,65,0, 4,20,0, 43,33,1]
mount_dangci1=[0,0,125, 0,93.4,31.6, 2.1,120.8,2.1, 1.9,123.1,0, 20.8,104.2,0, 69.8,53.6,1.6]
text1=dangci
text2=mount_dangci1
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_jiaoqu_level(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

 3.将城区楼盘数导入:

estate1['所在区'].value_counts()
chengqu=['朝阳','东城','海淀','西城','丰台','石景山']
mount=[38,3,15,2,35,15]
text1=chengqu
text2=mount
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_chengqu_mount(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

4. 将郊区楼盘数导入:

jiaoqu=['大兴','通州','顺义','平谷','门头沟','怀柔','密云','延庆','房山','昌平','燕郊','北京周边','香河']
mount=[52,39,43,9,24,9,11,4,33,23,24,67,10]
text1=jiaoqu
text2=mount
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_jiaoqu_mount(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

5. 将评论热度值(评论数/15)top5区域导入: 

area=['大兴','房山','北京周边','朝阳','丰台']
sizes=[9920,6081,5927,5402,5065]
for i in range(len(sizes)):
    sizes[i]=sizes[i]/15
    sizes[i]=int(sizes[i])
text1=area
text2=sizes
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_pinglun_mount(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

6. 各区楼盘热度(评论数、雷达图)导入:

labels=['东城', '丰台', '北京周边', '大兴', '密云', '平谷', '延庆', '怀柔', '房山', '昌平', '朝阳','海淀', '燕郊', '石景山', '西城', '通州', '门头沟', '顺义', '香河']
sizes=[136,5065,5927,9920,1516,988,277,629,6081,3959,5402,1412,2541,2042,234,4668,2910,4482,2126]

labels=['核心城区','中心城区','近郊区','远郊区','燕郊','北京周边']
sizes=[370,13921,25939,3410,2541,5979]
text1=labels
text2=sizes
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_area_heat(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

 7. 各区楼盘均价数据导入:

avg=estate1[['所在区','均价']].groupby(['所在区']).mean()
avg = avg.rename_axis('index').reset_index()
avg.columns=['所在区','均价']
list(avg["所在区"])
avg["均价"]=avg["均价"]/10000
avg["均价"]=avg["均价"].round(3)
import pymysql
conn = pymysql.connect(host='127.0.0.1',user='root',password='',database='temp202205231411')
cursor=conn.cursor()
text1=list(avg["所在区"])
text2=list(avg["均价"])
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_house_mount(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

8. 最贵的十个小区数据导入:

high_top10 = estate1[['小区名称','均价']].sort_values(by='均价',ascending=False)[:10] # 最高房价top10
low_top10 = estate1[['小区名称','均价']].sort_values(by='均价',ascending=True)[:10] # 最低房价top10
list(high_top10["均价"]/10000)
text1=list(high_top10["小区名称"])
text2=list(high_top10["均价"]/10000)
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_high_price(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

 9.  热度最高的别墅top5数据导入:

bieshu=['阳光城君山墅','新世界丽樽','凯德麓语','格拉斯墅区','御汤山']
price=[690,1888,690,1400,1800]
text1=bieshu
text2=price
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_redu_bieshu(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

10. 热度最高的住宅top7数据导入:

zhuzhai=['中海寰宇时代','中海寰宇视界','中海金樾和著','中海云筑','中海首开拾光里','观澜墅','中海北京世家']
price=[5.8,5.9,3.4,3.7,6,1.1,3.1]
text1=zhuzhai
text2=price
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_redu_house(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

11. 驾驶舱题目和楼盘总数导入:

title=['今日新楼盘数量','今日新楼盘数量','今日新楼盘数量','今日新楼盘数量','今日新楼盘数量']
mount=[0,0,4,5,6]
text1=title
text2=mount
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_title(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

 12. 购房指南数据导入:

zhinan=['商务楼多的地方住宅贵,住宅多的地方商务楼便宜','别以为签了购房合同就算完事,一定要催促开发商及时拿到房管局备案','即便有预售证,也得检查工程的工期和进度情况,能买现房就别买期房','认准当地房管部门的购房政策,政策才能决定你是否有买房资格']
date=['2022/06/15','2022/06/14','2022/06/13','2022/06/12']
text1=zhinan
text2=date
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_comment(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

  13. 房地产政策数据导入:

zhengce=['央行副行长潘功胜:房地产市场交易活跃性上升','坚持“房住不炒” 保障刚性住房需求','北京杭州一揽子措施稳经济,央行又释放稳楼市信号','中央政治局会议支持各地从当地实际出发完善房地产政策,支持刚性和改善性住房需求']
date=['2022/06/12','2022/06/10','2022/06/05','2022/05/29']
text1=zhengce
text2=date
for i in range(len(text1)):
    sql = "INSERT INTO mywebapp007_policy(text1,text2) VALUES (%s,%s)"
    myData = [(text1[i],text2[i])]
    cursor.executemany(sql,myData)
    conn.commit()

三、实现django驾驶舱

100套django驾驶舱模板:

链接:百度网盘 请输入提取码 
提取码:4a9y 

我选取的是1号模板,具体使用方法就是将配置文件添加到django中:

第一步:先在项目中新建一个文件夹static:

第二步:进行配置文件的迁移,直接在static文件夹中再新建一个文件夹index,将配置文件对应导入,其中index.html放在templates文件夹下:

​ 

各文件夹效果:

 第三步:修改index.html,将准备好的表格myList1至myList26导入到对应位置;

以下是我配置的index.html,由于代码太多了,还是给大家上个链接自取:

链接:https://pan.baidu.com/s/1I4tFS5aZYofZh1Ihnx0frQ 
提取码:ss1z 

拿来就可以直接替换index.html,适用于一号模板,其中包含了很多可以DIY的点,例如图形颜色、页面布局、数据刷新频率等等,如果有小伙伴想看,我以后会出文章讲解。

第四步:点击右上角绿色箭头,运行django项目。打开网址,展示效果:

可视化-数据中心

 成功显示!

总结

跟着上篇和下篇的顺序走一遍,一定可以成功实现驾驶舱,博主作为新手,很多地方处理得过于暴力低效,我也会继续优化~

猜你喜欢

转载自blog.csdn.net/weixin_50706330/article/details/127136033