Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

python - 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. Here is my application.py. I have already tried to get the input. Is a second web page in case of error necessary? Because the website itself isn't running in my IDE and I can't make out any errors.

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+'
' == 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 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>

i'm pretty new to coding, and i can't figure out what's going wrong

question from:https://stackoverflow.com/questions/65516761/why-is-my-flask-web-app-not-working-properly-i-cant-find-any-errors-myself

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. The following modifications and corrections:

1- use input instead of raw_input

2- use:

    f = open("words.txt", "r")
    dictionary = f.readlines()

instead of file('words.txt')

3- and of course use print(something) instead of print something.

4- adding to the end of your code the run command:

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=1)

5- Indentation in the function def check(x, y): should be corrected `

6- Adding the missing : in 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.

It would be good if add a sample input data and some lines of the file words.txt

Good Luck:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...