python
from flask import Flask, request, render_template
app = Flask(__name__)
players = ["Brave", "Warrior", "Wizard", "Ninja"]
@app.route("/")
def show():
message = "A new monster has appeared!"
return render_template("battle.html", message = message, players = players)
--This time, create a menu with the list created on the py side as an item.
--As for the list, it's just as it is
--Don't forget to put the list name in return
html
<form action="/result" method="post">
<select name="name">
{% for player in players %}
<option value="{{ player }}">{{ player }}</option>
{% endfor %}
</select>
<p></p>
<button type="submit">Fight</button>
</form>
--Declare to create a drop-down menu (also a select box) with the select tag
--For <option value =" {{player}} "> {{player}}
, ʻoption tagis for inputting menu items. <br> Here, the item name displayed using the for statement. And the name of value match --Now, when you press the
submit` button, the selected item will be entered. Convenient
--The txt data to be prepared should have a line break at the end.
python
from flask import Flask, request, render_template
import codecs
app = Flask(__name__)
@app.route("/")
def bbs():
file = codecs.open("articles.txt", "r", "utf-8")
lines = file.readlines()
file.close()
return render_template("bbs.html", lines = lines)
@app.route("/result", methods=["POST"])
def result():
article = request.form["article"]
name = request.form["name"]
return render_template("bbs.html", article = article, name = name)
--Declaring ʻimport codecs <br> It seems that you can read data with the specified character code by using codecs. --
file = codecs.open ("articles.txt", "r", "utf-8") The txt specified by
is put in the file variable
by utf-8 in r
mode.
--lines = file.readlines ()
reads all the data in a line-by-line list. Read everything
--The file is uncomfortable if it is left .open
, so let'sfile.close ()
--Don't forget the variable of return
html
{% for line in lines: %}
<p>{{ line.rstrip() }} </p>
{% endfor %}
--- Just display the data assigned to line one by one with the for statement
--- .rstrip ()
erases only one line feed code at the end of the line
――It's an html-like story that just arranges the appearance, so you don't have to touch py.
html
<table>
<tr><th>Post</th><th>name</th></tr>
{% for line in lines: %}
{% set column = line.rstrip().split(",") %}
<tr>
{% for item in column: %}
<td>{{ item }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
--Replace the html one above with this
--First, let's say that we will create a table with the table tag
.
--Declare that you will touch the contents of the horizontal axis with the tr tag
, and set the item name of the table with the th tag
in the tag.
--Use set
to put line data in column for each for trial.
This time, after deleting the line feed code at the end of the line with .rstrip
, use .split (",")
. Data is separated by ","
――After saying that you should put those data in the horizontal axis direction by tr tag
, put the single data separated by the for statement in ʻitem, and data with the tag for data input called
td tag` Enter
-↑ It's a little difficult to understand, so supplement
For example
txt
Hello World,peko
Hello Ruby,oz
Hello Sinatra,toramaru
Everyone in the world Konnichiha,Kirishima
Meow,cat
If it is txt data, in the first trial of for
--Data of Hello World, peko (line feed code)
in line
--Data called Hello World, peko
in the column (cleaning the data)
--Only the data Hello World
is included in the item (it was taken out by itself because it was separated)
Is included. Don't forget to declare that you can put it horizontally with the tr tag
.
――I know how to get a server on AWS, but what about getting a domain? ――Is it possible to use the free domain obtained elsewhere on AWS? ――I think you're saying something strange and you're wrong
Recommended Posts