在HTML页面中如何显示txt文件内容?

huangapple go评论75阅读模式
英文:

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(&#39;/&#39;)
def home():
    return render_template(&#39;index.html&#39;, 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 = &quot;database.db&quot;
DEFAULT_BUGGY_ID = &quot;1&quot;
BUGGY_RACE_SERVER_URL = &quot;https://rhul.buggyrace.net&quot;


file1 = open(&quot;costs.txt&quot;)
line = file1.readline()


#------------------------------------------------------------
# the index page
#------------------------------------------------------------

@app.route(&#39;/&#39;)
def home():
    return render_template(&#39;index.html&#39;, server_url=BUGGY_RACE_SERVER_URL, Line=line)

index.html -

&lt;h1&gt;
    My buggy editor
  &lt;/h1&gt;

  &lt;div class=&quot;spacer&quot;&gt;
    &lt;a href=&quot;/new&quot; class=&quot;button&quot;&gt;Make buggy&lt;/a&gt;
    &lt;a href=&quot;/buggy&quot; class=&quot;button&quot;&gt;Show buggy&lt;/a&gt;
    &lt;a href=&quot;/json&quot; class=&quot;button&quot;&gt;Get buggy JSON&lt;/a&gt;
  &lt;/div&gt;



  &lt;p&gt;
    

    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
    &lt;strong&gt;&lt;a href=&quot;{{ server_url }}&quot;&gt;race server&lt;/a&gt;&lt;/strong&gt;
    and enter your buggy into the next race.
  &lt;/p&gt;
  &lt;p&gt;
    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.
  &lt;/p&gt;

{% 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(&#39;index.html&#39;, 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(&quot;costs.txt&quot;)
line = file1.readline()

huangapple
  • 本文由 发表于 2023年5月30日 01:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定