英文:
How to display txt file contents in a HTML page?
问题
以下是您要翻译的内容:
First, to clarify, this question may have a lot of misunderstandings of how HTML works, sorry, it's my first time and I'm working with a pre-built program I've been provided with.
So I have a python based program, App.py, and I also have index.html
@app.route('/')
def home():
return render_template('index.html', server_url=BUGGY_RACE_SERVER_URL, Line=line)
when app is run, it's routing over to index.html.
Now I also have a txt file, which the contents of need to be displayed by index.html. On the Python side, I know how all the programming works, I can read the text, format it however I need, I just do not know how to pass that in or store it in such a way that I will be able to have index.html display it.
I'll put below the full code of both app.py and index.html in case they're needed. Sorry if I've missed anything important to explain or have not properly explained the issue, I'll answer any questions.
app.py -
from flask import Flask, render_template, request, jsonify
import os
import sqlite3 as sql
app = Flask(__name__)
DATABASE_FILE = "database.db"
DEFAULT_BUGGY_ID = "1"
BUGGY_RACE_SERVER_URL = "https://rhul.buggyrace.net"
file1 = open("costs.txt")
line = file1.readline()
# the index page
@app.route('/')
def home():
return render_template('index.html', server_url=BUGGY_RACE_SERVER_URL, Line=line)
index.html -
<h1>
My buggy editor
</h1>
<div class="spacer">
<a href="/new" class="button">Make buggy</a>
<a href="/buggy" class="button">Show buggy</a>
<a href="/json" class="button">Get buggy JSON</a>
</div>
<p>
Use this editor to specify a racing buggy. The editor saves it in its
little database and generates JSON data for the buggy. This is the data you
need to supply when you log into the
<strong><a href="{{ server_url }}">race server</a></strong>
and enter your buggy into the next race.
</p>
<p>
Remember that if your data is not accepted by the race server, your buggy
will be disqualified from that race... so make sure you program your editor
correctly.
</p>
{% endblock %}
英文:
First, to clarify, this question may have a lot of misunderstandings of how HTML works, sorry, it's my first time and I'm working with a pre-built program I've been provided with.
So I have a python based program, App.py, and I also have index.html
@app.route('/')
def home():
return render_template('index.html', server_url=BUGGY_RACE_SERVER_URL, Line=line)
when app is run, it's routing over to index.html.
Now I also have a txt file, which the contents of need to be displayed by index.html. On the Python side, I know how all the programming works, I can read the text, format it however I need, I just do not know how to pass that in or store it in such a way that I will be able to have index.html display it.
I'll put below the full code of both app.py and index.html in case they're needed. Sorry if I've missed anything important to explain or have not properly explained the issue, I'll answer any questions.
app.py -
from flask import Flask, render_template, request, jsonify
import os
import sqlite3 as sql
app = Flask(__name__)
DATABASE_FILE = "database.db"
DEFAULT_BUGGY_ID = "1"
BUGGY_RACE_SERVER_URL = "https://rhul.buggyrace.net"
file1 = open("costs.txt")
line = file1.readline()
#------------------------------------------------------------
# the index page
#------------------------------------------------------------
@app.route('/')
def home():
return render_template('index.html', server_url=BUGGY_RACE_SERVER_URL, Line=line)
index.html -
<h1>
My buggy editor
</h1>
<div class="spacer">
<a href="/new" class="button">Make buggy</a>
<a href="/buggy" class="button">Show buggy</a>
<a href="/json" class="button">Get buggy JSON</a>
</div>
<p>
Use this editor to specify a racing buggy. The editor saves it in its
little database and generates JSON data for the buggy. This is the data you
need to supply when you log into the
<strong><a href="{{ server_url }}">race server</a></strong>
and enter your buggy into the next race.
</p>
<p>
Remember that if your data is not accepted by the race server, your buggy
will be disqualified from that race... so make sure you program your editor
correctly.
</p>
{% endblock %}
答案1
得分: 1
你离成功已经很接近了。看看这行代码:
return render_template('index.html', server_url=BUGGY_RACE_SERVER_URL, Line=line)
在这里,你正在渲染你的 index.html 模板,并传递一些内容给它,server_url
就是其中之一(这已经在你的 HTML 中显示了),Line
是另一个。
所以,你需要将以下内容放在你的 index.html 中,你希望显示这个内容的地方:
{{ Line }}
这将在你的网页上显示你从文件中加载的行,因为你有以下代码:
file1 = open("costs.txt")
line = file1.readline()
英文:
You are almost there. Look at this line
return render_template('index.html', server_url=BUGGY_RACE_SERVER_URL, Line=line)
Here, you are rendering your index.html template and pass some content to it, server_url
being one such content (and that's already displayed in your HTML) and Line
being another.
So, you will need to put
{{ Line }}
to the place in your index.html where you want this content to be displayed at. It should display the line you have loaded from the file because of your
file1 = open("costs.txt")
line = file1.readline()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论