为什么瓶子不能显示本地图像?
Why bottle can not display local image?
我是一个beginner.I想用瓶子展示一个picture.In事实,我先存图
然后我想用我的代码做的时候div.But中的图片
错误信息是"GET http://localhost:8081/test.jpg 404 (Not Found)"
我的项目结构:
--testBottle.py
--test.jpg
testBottle.py中的代码:
# -*- coding: utf-8 -*-
import bottle
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from scipy import optimize as opt
def generate(code, year,week):
kion = pd.read_csv(r'D:/a.csv')
kion.head()
Px = np.arange(0, len(kion), 1)
Py = kion['temp']
plt.plot(Px, Py)
res = opt.curve_fit(fit_func, Px, Py)
a = res[0][0]
b = res[0][1]
c = res[0][2]
d = res[0][3]
print("a = %s" % a)
print("b = %s" % b)
print("c = %s" % c)
print("d = %s" % d)
Px2 = []
for x in Px:
Px2.append(a * x ** 3 + b * x ** 2 + c * x + d)
plt.plot(Px, Py)
plt.plot(Px, np.array(Px2))
plt.savefig('test.jpg')
bottle.redirect('/show')
def fit_func(x, a, b, c, d):
return a * x ** 3 + b * x ** 2 + c * x + d
@bottle.route('/show')
def index():
return ''' <div id="container" style="height: 200px; width:200px">
<img src="/test.jpg" alt="error" />
</div>
'''
@bottle.route('/index')
def index():
return ''' <form action="/generate" method="post">
enployeeCode: <input name="enployeeCode" type="text" /><br/>
reportYear: <input name="reportYear" type="text" /><br/>
reportWeek: <input name="reportWeek" type="text" /><br/>
<input value="generate" type="submit">
</form>
'''
@bottle.route('/generate', method='POST')
def get_para():
enployeeCode = bottle.request.POST.get('enployeeCode')
reportYear = bottle.request.POST.get('reportYear')
reportWeek = bottle.request.POST.get('reportWeek')
if enployeeCode and reportYear and reportWeek:
generate(enployeeCode, reportYear,reportWeek)
bottle.run(host='localhost', port=8081)
我在URL中输入:http://localhost:8081/index
然后我点击按钮。
连我把图片路径改成绝对路径是这样的:
<img src="E://test//test.jpg" alt="error" />
也显示不了
要提供静态文件或图像,您必须创建路由。
@bottle.route('/静态/')
def server_static(文件名):
"""
提供静态文件,如 .js、.css、.jpeg 等...
"""
return static_file(文件名, root='static/')
在这个例子中,创建文件夹/static/
并通过 url /static/
测试每个文件
我知道怎么做了。
修改代码如下:
testBottle.py中的代码:
# -*- coding: utf-8 -*-
import bottle
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from scipy import optimize as opt
import os
def generate(code, year, week):
kion = pd.read_csv(r'D:/a.csv')
kion.head()
Px = np.arange(0, len(kion), 1)
Py = kion['temp']
plt.plot(Px, Py)
res = opt.curve_fit(fit_func, Px, Py)
a = res[0][0]
b = res[0][1]
c = res[0][2]
d = res[0][3]
# print("a = %s" % a)
# print("b = %s" % b)
# print("c = %s" % c)
# print("d = %s" % d)
Px2 = []
for x in Px:
Px2.append(a * x ** 3 + b * x ** 2 + c * x + d)
plt.plot(Px, Py)
plt.plot(Px, np.array(Px2))
plt.savefig('./image/test.jpg')
bottle.redirect('/show'+'test')
def fit_func(x, a, b, c, d):
return a * x ** 3 + b * x ** 2 + c * x + d
@bottle.route('/show<name>')
def server_static(name):
return bottle.static_file(name+'.jpg', root='./image')
@bottle.route('/index')
def index():
# currentPath = os.path.dirname(__file__)
# return bottle.template(currentPath+r'/html/index.html')
return bottle.template('./html/index.html')
@bottle.route('/css/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./css')
@bottle.route('/js/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./js')
@bottle.route('/fonts/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./fonts')
@bottle.route('/image/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./image')
# @bottle.route('/locales/<filename>')
# def server_static(filename):
# return bottle.static_file(filename, root='./locales')
@bottle.route('/generate', method='POST')
def get_para():
employeeCode = bottle.request.POST.get('employeeCode')
reportYear = bottle.request.POST.get('reportYear')
reportWeek = bottle.request.POST.get('reportWeek')
print("employeeCode:"+str(employeeCode))
print("reportYear:" +str(reportYear))
print("reportWeek:" + str(reportWeek))
if employeeCode and reportYear and reportWeek:
generate(employeeCode, reportYear,reportWeek)
return "sucess"
else:
return "fail"
@bottle.error(404)
def error404(error):
return 'Nothing here, sorry'
bottle.run(host='localhost', port=8081)
index.html中的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/jquery-ui.css">
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery-ui.js"></script>
<script src="../js/bootstrap.min.js"></script>
</head>
<body>
<form action="/generate" method="post">
enployeeCode: <input name="employeeCode" type="text" /><br/>
reportYear: <input name="reportYear" type="text" /><br/>
reportWeek: <input name="reportWeek" type="text" /><br/>
<input value="generate" type="submit">
</form>
</body>
</html>
我是一个beginner.I想用瓶子展示一个picture.In事实,我先存图
然后我想用我的代码做的时候div.But中的图片
错误信息是"GET http://localhost:8081/test.jpg 404 (Not Found)"
我的项目结构:
--testBottle.py
--test.jpg
testBottle.py中的代码:
# -*- coding: utf-8 -*-
import bottle
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from scipy import optimize as opt
def generate(code, year,week):
kion = pd.read_csv(r'D:/a.csv')
kion.head()
Px = np.arange(0, len(kion), 1)
Py = kion['temp']
plt.plot(Px, Py)
res = opt.curve_fit(fit_func, Px, Py)
a = res[0][0]
b = res[0][1]
c = res[0][2]
d = res[0][3]
print("a = %s" % a)
print("b = %s" % b)
print("c = %s" % c)
print("d = %s" % d)
Px2 = []
for x in Px:
Px2.append(a * x ** 3 + b * x ** 2 + c * x + d)
plt.plot(Px, Py)
plt.plot(Px, np.array(Px2))
plt.savefig('test.jpg')
bottle.redirect('/show')
def fit_func(x, a, b, c, d):
return a * x ** 3 + b * x ** 2 + c * x + d
@bottle.route('/show')
def index():
return ''' <div id="container" style="height: 200px; width:200px">
<img src="/test.jpg" alt="error" />
</div>
'''
@bottle.route('/index')
def index():
return ''' <form action="/generate" method="post">
enployeeCode: <input name="enployeeCode" type="text" /><br/>
reportYear: <input name="reportYear" type="text" /><br/>
reportWeek: <input name="reportWeek" type="text" /><br/>
<input value="generate" type="submit">
</form>
'''
@bottle.route('/generate', method='POST')
def get_para():
enployeeCode = bottle.request.POST.get('enployeeCode')
reportYear = bottle.request.POST.get('reportYear')
reportWeek = bottle.request.POST.get('reportWeek')
if enployeeCode and reportYear and reportWeek:
generate(enployeeCode, reportYear,reportWeek)
bottle.run(host='localhost', port=8081)
我在URL中输入:http://localhost:8081/index
然后我点击按钮。
连我把图片路径改成绝对路径是这样的:
<img src="E://test//test.jpg" alt="error" />
也显示不了
要提供静态文件或图像,您必须创建路由。 @bottle.route('/静态/') def server_static(文件名): """ 提供静态文件,如 .js、.css、.jpeg 等... """ return static_file(文件名, root='static/')
在这个例子中,创建文件夹/static/ 并通过 url /static/
测试每个文件我知道怎么做了。
修改代码如下:
testBottle.py中的代码:
# -*- coding: utf-8 -*-
import bottle
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from scipy import optimize as opt
import os
def generate(code, year, week):
kion = pd.read_csv(r'D:/a.csv')
kion.head()
Px = np.arange(0, len(kion), 1)
Py = kion['temp']
plt.plot(Px, Py)
res = opt.curve_fit(fit_func, Px, Py)
a = res[0][0]
b = res[0][1]
c = res[0][2]
d = res[0][3]
# print("a = %s" % a)
# print("b = %s" % b)
# print("c = %s" % c)
# print("d = %s" % d)
Px2 = []
for x in Px:
Px2.append(a * x ** 3 + b * x ** 2 + c * x + d)
plt.plot(Px, Py)
plt.plot(Px, np.array(Px2))
plt.savefig('./image/test.jpg')
bottle.redirect('/show'+'test')
def fit_func(x, a, b, c, d):
return a * x ** 3 + b * x ** 2 + c * x + d
@bottle.route('/show<name>')
def server_static(name):
return bottle.static_file(name+'.jpg', root='./image')
@bottle.route('/index')
def index():
# currentPath = os.path.dirname(__file__)
# return bottle.template(currentPath+r'/html/index.html')
return bottle.template('./html/index.html')
@bottle.route('/css/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./css')
@bottle.route('/js/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./js')
@bottle.route('/fonts/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./fonts')
@bottle.route('/image/<filename>')
def server_static(filename):
return bottle.static_file(filename, root='./image')
# @bottle.route('/locales/<filename>')
# def server_static(filename):
# return bottle.static_file(filename, root='./locales')
@bottle.route('/generate', method='POST')
def get_para():
employeeCode = bottle.request.POST.get('employeeCode')
reportYear = bottle.request.POST.get('reportYear')
reportWeek = bottle.request.POST.get('reportWeek')
print("employeeCode:"+str(employeeCode))
print("reportYear:" +str(reportYear))
print("reportWeek:" + str(reportWeek))
if employeeCode and reportYear and reportWeek:
generate(employeeCode, reportYear,reportWeek)
return "sucess"
else:
return "fail"
@bottle.error(404)
def error404(error):
return 'Nothing here, sorry'
bottle.run(host='localhost', port=8081)
index.html中的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/jquery-ui.css">
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery-ui.js"></script>
<script src="../js/bootstrap.min.js"></script>
</head>
<body>
<form action="/generate" method="post">
enployeeCode: <input name="employeeCode" type="text" /><br/>
reportYear: <input name="reportYear" type="text" /><br/>
reportWeek: <input name="reportWeek" type="text" /><br/>
<input value="generate" type="submit">
</form>
</body>
</html>