[英]Why is my flask web app not working properly? I can't find any errors myself
So I tried to write my first flask web application and the website isn't working.所以我尝试编写我的第一个 flask web 应用程序,但该网站无法正常工作。 Here is my application.py.
这是我的 application.py。 I have already tried to get the input.
我已经尝试获取输入。 Is a second web page in case of error necessary?
如果出现错误,是否需要第二个 web 页面? Because the website itself isn't running in my IDE and I can't make out any errors.
因为网站本身没有在我的 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")
and here is my 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>
i'm pretty new to coding, and i can't figure out what's going wrong我对编码很陌生,我不知道出了什么问题
I have tried to run your code, so I made some modifications to make it run on my python 3.7 in the windows operating system.我试过运行你的代码,所以我做了一些修改,让它在我的 windows 操作系统上运行 python 3.7。 The following modifications and corrections:
以下修改和更正:
1- use input
instead of raw_input
1- 使用
input
而不是raw_input
2- use: 2- 使用:
f = open("words.txt", "r")
dictionary = f.readlines()
instead of file('words.txt')
而不是
file('words.txt')
3- and of course use print(something)
instead of print something
. 3-当然使用
print(something)
而不是print something
。
4- adding to the end of your code the run command: 4- 在代码末尾添加运行命令:
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=1)
5- Indentation in the function def check(x, y):
should be corrected ` 5- function
def check(x, y):
应该更正`
6- Adding the missing :
in if request.form.get("Word")
6- 添加缺失的
:
在if request.form.get("Word")
Finally, the program runs, but you use input from console like raw_input
or input
, meanwhile, you run Flask
web server, so you should use forms to get the user inputs.最后,程序运行,但您使用来自控制台的输入,如
raw_input
或input
,同时,您运行Flask
web 服务器,因此您应该使用 forms 来获取用户输入。
It would be good if add a sample input data and some lines of the file words.txt
如果添加示例输入数据和文件
words.txt
的一些行会很好
Good Luck:祝你好运:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.