gpt4 book ai didi

python bottle,找不到外部的js和css

转载 作者:行者123 更新时间:2023-11-28 19:03:24 26 4
gpt4 key购买 nike

我在 bottle 中使用 html,在“index.html”中我导入外部 JS 和 CSS。

但是加载页面时,找不到css和js。

enter image description here

我的项目结构:

enter image description here

testBottle.py 中的代码:

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]
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():
return bottle.template('./html/index.html')
@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)

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="enployeeCode" 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>

如何修改代码让HTML加载JS和CSS?

最佳答案

我自己解决的,添加如下代码:

@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')

然后就没有报错了。

完整代码:

# -*- 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('/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.error(404)
def error404(error):
return 'Nothing here, sorry'

bottle.run(host='localhost', port=8081)

关于python bottle,找不到外部的js和css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50013288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com