51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1771|回复: 1
打印 上一主题 下一主题

使用Django和heroku搭建一个简易网站

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-2-12 16:26:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近由于毕设的需要,想要做一个通过输入各种参数,预测冷热负荷的机器学习系统。可以在网页上直接输入房屋参数,预测出房屋所需冷热负荷和能耗水平。

关于机器学习那块相信网上已经有不少教程,这里就不再赘述,本文主要总结一下Django和heroku搭配来建立网站。

Django
这个是根据tutorial建立起来的网站的文件目录。
  1. mysite/
  2.     manage.py
  3.     mysite/
  4.         __init__.py
  5.         settings.py
  6.         urls.py
  7.         wsgi.py
  8.     polls/
  9.         __init__.py
  10.         admin.py
  11.         migrations/
  12.             __init__.py
  13.             0001_initial.py
  14.         models.py
  15.         static/
  16.             polls/
  17.                 images/
  18.                     background.gif
  19.                 style.css
  20.         templates/
  21.             polls/
  22.                 detail.html
  23.                 index.html
  24.                 results.html
  25.         tests.py
  26.         urls.py
  27.         views.py
  28.     templates/
  29.         admin/
  30.             base_site.html
复制代码
最顶层是mysite使我们的project,然后在这个project下面添加了一个app叫做polls。

在project下面又有一个文件夹叫mysite,里面放了一些setting,urls的重要文件。
url是指明Uniform Resource Locator的文件,即各种资源的索引位置。
wsgi.py是方便之后部署的文件。

在polls文件夹中,migration文件夹主要是负责数据库的连接。
models文件里定义了一些类,是我们数据库中会记录的东西。
views是我们给访问者呈现什么数据,它和templates是搭配的。
templates文件夹里,还有一个polls文件夹(虽然我自己后来建的时候放在polls文件夹中的html文件总是找不到就不再加这个polls子文件夹了,但是合理的情况是放在polls中的),存放着html格式的文件,和views搭配负责页面如何呈现。

其中在我自己的project里面最重要的是表单,即用户输入数据然后表单通过request接收,views.py处理,return回去。
这是我views.py文件的内容:
  1. # -*- coding: utf-8 -*-
  2. from django.shortcuts import render, render_to_response
  3. from django.views.decorators import csrf
  4. import tensorflow as tf
  5. import numpy as np


  6. def add_layer(inputs, insize, outsize, n, activation_function=None):
  7.     layer_name = 'layer%s' % n
  8.     with tf.name_scope(layer_name):
  9.         Weights = tf.Variable(tf.random_normal([insize, outsize]), name='w')
  10.         tf.summary.histogram(layer_name + 'Weights', Weights)
  11.         bias = tf.Variable(tf.zeros([1, outsize]))
  12.         tf.summary.histogram(layer_name + 'bias', bias)
  13.         wx_b = tf.add(tf.matmul(inputs, Weights), bias)

  14.         if activation_function is None:
  15.             output = wx_b
  16.         else:
  17.             output = activation_function(wx_b, )
  18.         return output

  19. def input(request):
  20.     return render_to_response('get.html')

  21. def calculate(request):
  22.     # xdata = [0.98, 514.50, 294.00, 110.25, 7.00, 2, 0.10, 1]
  23.     request.encoding = 'utf-8'
  24.     context = {}
  25.     xdata = []
  26.     if request.GET:
  27.         for num in range(1, 9):
  28.             xdata.append(float(request.GET['X%i' % num]))

  29.     xdata = np.array(xdata).reshape([1, 8])
  30.     xdata = 1.0 / (1 + np.exp(xdata))
  31.     # print('xdata: ', xdata)
  32.     # context['heatload'] = xdata[0]
  33.     # context['coolload'] = xdata[2]
  34.    
  35.     tf.reset_default_graph()
  36.     xs = tf.placeholder(tf.float32, xdata.shape, name='xinput')

  37.     l1 = add_layer(xs, xdata.shape[1], 10, 1, activation_function=tf.nn.relu)  # 10 is layer units
  38.     l2 = add_layer(l1, 10, 10, 2, activation_function=tf.nn.relu)  # 10 is layer units
  39.     prediction = add_layer(l2, 10, 2, 3, activation_function=None)  # predicted output

  40.     saver = tf.train.Saver()

  41.     with tf.Session() as sess:
  42.         # you cannot initialize here
  43.         saver.restore(sess, './my_net/save_net.ckpt')
  44.         rlt = sess.run(prediction, feed_dict={xs: xdata})
  45.         # print('result: ', rlt)
  46.         context['heatload'] = rlt[0, 0]
  47.         context['coolload'] = rlt[0, 1]
  48.         print('context', context)
  49.     return render(request, "result.html", context)
复制代码
get.html
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/html">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Calculate the hear load and cool load</title>
  6. </head>
  7. <body>
  8.     <form action = "/result/" method = "get">
  9.         {%  csrf_token %}
  10.         Relative Compactness (surface-area-to-volume ratio): <input type="number" step = "any" name="X1" value = 0.764167> <br />
  11.         Surface Area: <input type="number" name="X2" step = "any" value = 671.708333> <br />
  12.         Wall Area: <input type="number" name="X3" step = "any" value = 318.50000> <br />
  13.         Roof Area: <input type="number" name="X4" step = "any" value = 176.604167> <br />
  14.         Overall Height: <input type="number" name="X5" step = "any" value = 5.25> <br />
  15.         Orientation: <input type="number" name="X6" step = "any" value = 3.5> <br />
  16.         Glazing Area: <input type="number" name="X7" step = "any" value = 0.234375> <br />
  17.         Glazing Area Distribution: <input type="text" name="X8" step = "any" value = 2.812500> <br />
  18.         <input type="submit" value="Submit">
  19.     </form>
  20. <p></p>

  21. </body>
  22. </html>
复制代码
返回结果的页面get.html。里面还用到一些html判断语句来判断能耗等级。
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>result</title>
  6. </head>
  7. <body>
  8. heat load: <p> {{ heatload }} BTU </p> <br />
  9. cool load: <p> {{ coolload }} BTU </p> <br />

  10. {% if heatload < 11.67 and coolload < 14.52 %}
  11.     the efficiency classification: very low heating and cooling requirement
  12. {% elif 15.92 > heatload and heatload >= 11.67 and 18.65 > coolload and coolload >= 14.52  %}
  13.     the efficiency classification: low heating and cooling requirement
  14. {% elif 26.27 > heatload and heatload >= 15.92 and 28.27 > coolload and coolload >= 18.65  %}
  15.     the efficiency classification: medium heating and cooling requirement
  16. {% elif 32.32 > heatload and heatload >= 26.27 and 34.03 > coolload and coolload >= 28.27  %}
  17.     the efficiency classification: high heating and cooling requirement
  18. {% else %}
  19.     the efficiency classification: very high heating and cooling requirement
  20. {% endif %}
  21. </body>
  22. </html>
复制代码
然后下面这个是mysite/urls,是指明了url和views或者下面app的url关联关系
  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from energy.views import calculate

  4. urlpatterns = [
  5.     path('', include('energy.urls')),
  6.     path('result/', calculate),
  7.     path('admin/', admin.site.urls),
  8. ]
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-20 06:26 , Processed in 0.063325 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表