簡體   English   中英

為什么我的 flask web 應用程序不能正常工作? 我自己找不到任何錯誤

[英]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>&nbsp;&nbsp;&nbsp;<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_inputinput ,同時,您運行Flask web 服務器,因此您應該使用 forms 來獲取用戶輸入。

如果添加示例輸入數據和文件words.txt的一些行會很好

祝你好運:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM