[英]Why is my flask web app not working properly? I can't find any errors myself
所以我尝试编写我的第一个 flask web 应用程序,但该网站无法正常工作。 这是我的 application.py。 我已经尝试获取输入。 如果出现错误,是否需要第二个 web 页面? 因为网站本身没有在我的 IDE 中运行,所以我无法找出任何错误。
from flask import Flask, render_template, request
import requests
import json
from tempfile import mkdtemp
from flask_session import Session
import random
app = Flask(__name__)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# import permutation module
from itertools import permutations as prm
scrambled_word = list(str(raw_input("Input your jumbled word: ")))
# empty lists that will be appended to later
prm_list = []
possible_words = []
# take each permutation of the input and put in a list
for i in prm(scrambled_word):
prm_list.append("".join(i))
print i
def check(x, y):
# open list of words
dictionary = file('words.txt')
# check each line in the dictionary against each item
# in the list of permutations and add it to another empty list if it's a match
for line in dictionary:
for i in x:
if i+'\n' == line:
y.append(i)
check(prm_list, possible_words)
# delete duplicates
possible_words = list(set(possible_words))
# print out possible words
if len(possible_words) == 0 or len(possible_words) == 1 or len(possible_words) == 2 or len(possible_words) == 3:
print "No match found"
else:
for i in possible_words:
print "Possible Word for Jumbled Word is: " + i
@app.route("/", methods=["GET", "POST"])
def index():
# make sure that method is POST
if request.method == "POST":
app_id = 'fbea320c'
app_key = '436eab6eae3dbeec6ec311328b9ff7dd'
language = 'en'
if request.form.get("Word")
return render_template("final.html")
这是我的决赛。html
<html lang="en">
<head>
<style>
input{
max-width: 350px;
}
select{
max-width: 400px;
}
#ABC {
background-color: black;
position: absolute;
width: 95%;
}
#imh {
width: 100%;
height: 50%;
}
.centered {
position: absolute;
top: 25%;
left: 50%;
transform: translate(-50%, -50%);
color: #ffffff;
text-align: center;
}
</style>
<title>Word Finder</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<img src="/static/jumble.jpeg" id="imh">
<div class="centered"><font size="7">Jumble Solver</font><font size="4"><p>Unscramble words!</p></font></div>
<div class="container">
<br><div class="row">
<center><h1>Enter a Word</h1></center><br><br></div><div class="row">
<div class="inpot"><center>
<form method="post" action="/">
<div class="form-group">
{% if a == 0 %}
<input type="text" name="Word" id="Word" autofocus autocomplete="off" class="form-control" placeholder="Enter a Word" onclick="return IsEmpty()" value="{{ j }}"></div>
{% else %}
<input type="text" name="Word" id="Word" autofocus autocomplete="off" class="form-control" placeholder="Enter a Word"/></div>
{% endif %}
<div class="form-group">
</div><input class="btn btn-danger" type="submit" name="submit" value="submit"></button> <input class="btn btn-danger" name="submit" value="next" type="submit"></button></form>
<div class="row">
<div class=" text-center">
<h3>Search up any word!</h3>
<p>With our jumble solver you can unscramble any mix of letters!</p>
</div>
<div class="col-sm-4 text-center">
</div>
<div class="col-sm-4 text-center">
</div></div>
</div></div></font></body></html>
我对编码很陌生,我不知道出了什么问题
我试过运行你的代码,所以我做了一些修改,让它在我的 windows 操作系统上运行 python 3.7。 以下修改和更正:
1- 使用input
而不是raw_input
2- 使用:
f = open("words.txt", "r")
dictionary = f.readlines()
而不是file('words.txt')
3-当然使用print(something)
而不是print something
。
4- 在代码末尾添加运行命令:
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=1)
5- function def check(x, y):
应该更正`
6- 添加缺失的:
在if request.form.get("Word")
最后,程序运行,但您使用来自控制台的输入,如raw_input
或input
,同时,您运行Flask
web 服务器,因此您应该使用 forms 来获取用户输入。
如果添加示例输入数据和文件words.txt
的一些行会很好
祝你好运:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.