Flask index.html 未在本地主机上加载
我从 https://github.com/kdelwat/Onset 构建了一个 Flask 存储库,并遵循了所有README 中的说明,但是当我 cd 'run.py' 并运行 Flask 的开发服务器时,我转到本地主机 (http://127.0.0.1:5000/),它什么也不加载,只是一个空白页面。我快速查看了 index.html 文件,一切似乎都运行正常。在那次事件发生之前,当我进入该目录时,没有模板文件,因此我在该目录中创建了一个“templates”文件并将index.html移到了那里,但随后出现了无模板错误。然后我将模板文件移动到“app”文件夹中,错误消失了,所以我再次运行 Flask 服务器,但我仍然在这里。
在编写时进行编辑,
我遇到了 HTML 编译器,我认为当我运行 index.html 时,代码本身只是标题,没有其他内容。我很困惑。我应该在模板文件夹中放入什么?是不是注射有问题?请帮忙。
这是 run.py 代码:
from app import app
app.run(debug=True)
根据 Michael Amadi 的要求,这是我的 目录结构。 PS 我对 Python 和编码都很陌生,所以也许我根本不知道如何设置 Web 应用程序。 init.py:
from flask import Flask
app=Flask(__name__)
from app import views
视图.py:
import json
import sys
import os.path as path
from flask import render_template, jsonify, request
from app import app
base_directory = path.dirname(path.dirname(path.abspath(__file__)))
sys.path.append(path.join(base_directory, 'engine'))
import engine
import metrics
optimisation_functions = {'Maximise': max, 'Minimise': min}
metric_functions = {'weighted phonetic product': metrics.weighted_phonetic_product,
'phonetic product': metrics.phonetic_product,
'Word Complexity Measure': metrics.word_complexity_measure,
'number of syllables': metrics.number_of_syllables,
'number of consonant clusters': metrics.number_of_consonant_clusters,
'random value': metrics.random_value}
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html')
def sanitise(string):
'''Remove all whitespace from a string and lowercase it.'''
banned_characters = "'.-–—‒ \u02C8"
return ''.join([c for c in string.strip().lower() if c not in
banned_characters])
def format_transcriptions(transcriptions):
'''Split the raw string of transcriptions into
the correct tuple rules.'''
clean_transcriptions = transcriptions.strip().lower()
if len(clean_transcriptions) == 0:
return []
else:
return [(sanitise(pair.split(':')[0]), sanitise(pair.split(':')[1]))
for pair in clean_transcriptions.split('\n')]
@app.route('/evolve')
def evolve():
words = [sanitise(word) for word in request.args['words'].split()]
try:
transcriptions = format_transcriptions(request.args['transcriptions'])
except IndexError:
return jsonify({'error': 'Transcription seperator must be a colon'})
try:
generations = int(request.args['generations'])
except ValueError:
return jsonify({'error': 'Generations must be an integer'})
if request.args['direction'] == 'Reverse':
reverse = True
else:
reverse = False
optimisation_function = optimisation_functions[request.args['optimisationFunction']]
metric = metric_functions[request.args['metric']]
try:
words, rules = engine.run_engine(words, generations, transcriptions,
reverse, metric, optimisation_function)
except Exception as e:
return jsonify({'error': str(e)})
return jsonify({'rules': rules, 'words': words, 'error': 0})
@app.route('/apply')
def apply():
'''Evolves the language according to the given rules, specified by:
words: list [strings]
rules: list [Rules]
reverse: if True, apply in reverse order (used when applying rules
created by reverse evolution)
'''
words = [sanitise(word) for word in request.args['words'].split()]
rules = json.loads(request.args['rules'])
if request.args['direction'] == 'Reverse':
reverse = True
else:
reverse = False
try:
transcriptions = format_transcriptions(request.args['transcriptions'])
except IndexError:
return jsonify({'error': 'Transcription seperator must be a colon'})
try:
evolved_words = engine.apply_rules(words, rules, transcriptions,
reverse)
except Exception as e:
return jsonify({'error': str(e)})
return jsonify({'words': evolved_words, 'error': 0})
I built a flask repository from https://github.com/kdelwat/Onset and followed all of the instructions from the README, but when I cd the 'run.py' and run Flask's development server, I go to the localhost (http://127.0.0.1:5000/) and it loads up nothing, just a blank page. I took a quick look at the index.html file and everything seems to be running okay. Before that incident happened, when I went into the directory there was no template file so I created a "templates" file in the directory and moved the index.html there, but then there was a no-template error. Then I moved the templates file into the 'app' folder and the error was gone, so I run the Flask server again and I am still here.
Edit while writing
I ran in on an HTML compiler and I think that when I run the index.html the code itself is just the title with nothing else. I am very confused. What am I supposed to put in templates folder? Is there a problem with the injecting? Please help.
Here is the run.py code:
from app import app
app.run(debug=True)
As requested from Michael Amadi, here is my directory structure.
P.S I am new to Python and coding in general, so maybe I just don't know how to set up a Web-app at all.
init.py:
from flask import Flask
app=Flask(__name__)
from app import views
views.py:
import json
import sys
import os.path as path
from flask import render_template, jsonify, request
from app import app
base_directory = path.dirname(path.dirname(path.abspath(__file__)))
sys.path.append(path.join(base_directory, 'engine'))
import engine
import metrics
optimisation_functions = {'Maximise': max, 'Minimise': min}
metric_functions = {'weighted phonetic product': metrics.weighted_phonetic_product,
'phonetic product': metrics.phonetic_product,
'Word Complexity Measure': metrics.word_complexity_measure,
'number of syllables': metrics.number_of_syllables,
'number of consonant clusters': metrics.number_of_consonant_clusters,
'random value': metrics.random_value}
@app.route('/')
@app.route('/index')
def index():
return render_template('index.html')
def sanitise(string):
'''Remove all whitespace from a string and lowercase it.'''
banned_characters = "'.-–—‒ \u02C8"
return ''.join([c for c in string.strip().lower() if c not in
banned_characters])
def format_transcriptions(transcriptions):
'''Split the raw string of transcriptions into
the correct tuple rules.'''
clean_transcriptions = transcriptions.strip().lower()
if len(clean_transcriptions) == 0:
return []
else:
return [(sanitise(pair.split(':')[0]), sanitise(pair.split(':')[1]))
for pair in clean_transcriptions.split('\n')]
@app.route('/evolve')
def evolve():
words = [sanitise(word) for word in request.args['words'].split()]
try:
transcriptions = format_transcriptions(request.args['transcriptions'])
except IndexError:
return jsonify({'error': 'Transcription seperator must be a colon'})
try:
generations = int(request.args['generations'])
except ValueError:
return jsonify({'error': 'Generations must be an integer'})
if request.args['direction'] == 'Reverse':
reverse = True
else:
reverse = False
optimisation_function = optimisation_functions[request.args['optimisationFunction']]
metric = metric_functions[request.args['metric']]
try:
words, rules = engine.run_engine(words, generations, transcriptions,
reverse, metric, optimisation_function)
except Exception as e:
return jsonify({'error': str(e)})
return jsonify({'rules': rules, 'words': words, 'error': 0})
@app.route('/apply')
def apply():
'''Evolves the language according to the given rules, specified by:
words: list [strings]
rules: list [Rules]
reverse: if True, apply in reverse order (used when applying rules
created by reverse evolution)
'''
words = [sanitise(word) for word in request.args['words'].split()]
rules = json.loads(request.args['rules'])
if request.args['direction'] == 'Reverse':
reverse = True
else:
reverse = False
try:
transcriptions = format_transcriptions(request.args['transcriptions'])
except IndexError:
return jsonify({'error': 'Transcription seperator must be a colon'})
try:
evolved_words = engine.apply_rules(words, rules, transcriptions,
reverse)
except Exception as e:
return jsonify({'error': str(e)})
return jsonify({'words': evolved_words, 'error': 0})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论