'how to import data to html list with flask
I need to import data in an python variable to html list this is part of my html and you can run it ..
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Panel</title>
</head>
<style>
@import url('https://v1.fontapi.ir/css/Vazir');
body,html,.container{margin: 0; width: 100%; height: 100%;font-family: "vazir",tahoma;}
.container {
font-family: "vazir";
display: grid;
grid-template-columns: 1.7fr 0.9fr;
grid-template-rows: 1fr 1fr;
gap: 0px 0px;
grid-auto-flow: row;
grid-template-areas:
"addbot botstatus"
"sendmsg blockuser";
border-radius: 23px;
background: #e0e0e0;
box-shadow: 29px 29px 58px #bebebe,
-29px -29px 58px #ffffff;
}
.addbot {
text-align: left;
color: rgb(68, 68, 68);
font-family: 'vazir';
grid-area: addbot;
border-radius: 23px;
background: #e0e0e0;
box-shadow: 29px 29px 58px #bebebe,
-29px -29px 58px #ffffff;
margin: 23px;
}
.addbot:hover{transform: scale(1);}.sendmsg:hover{transform: scale(1);}
.botstatus { grid-area: botstatus;
border-radius: 23px;
background: #e0e0e0;
box-shadow: 29px 29px 58px #bebebe,
-29px -29px 58px #ffffff;
margin: 23px;
}
.sendmsg { grid-area: sendmsg;
border-radius: 23px;
background: #e0e0e0;
box-shadow: 29px 29px 58px #bebebe,
-29px -29px 58px #ffffff;
margin: 23px;
}
.blockuser { grid-area: blockuser; margin: 23px;
border-radius: 23px;
background: #e0e0e0;
box-shadow: 29px 29px 58px #bebebe,
-29px -29px 58px #ffffff;}
.blockuser:hover{transform: scale(1);}
.botstatus{transform: scale(1);}
input{
padding: 5px;
font-size: 16px;
border-width: 3px;
border-color: #CCCCCC;
background-color: #FFFFFF;
color: #000000;
border-style: solid;
border-radius: 0px;
box-shadow: 0px 0px 0px rgba(66,66,66,.75);
text-shadow: 0px 0px 0px rgba(66,66,66,.75);
}
input:focus{
border: none;
}
.addform{
text-align: center;
}
.delbot{text-align: center;}
.addbot {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr;
gap: 0px 0px;
grid-template-areas:
"addbot delbot";
}
.addform { grid-area: addbot; }
.delbot { grid-area: delbot; }
.botstatus {text-align: center;color: rgb(68, 68, 68);
}
.green{color: rgb(97, 209, 97);
font-weight: bold;
}
.lists{text-align: justify; }
</style>
<body>
<div class="container">
<div class="addbot">
<div class="addform">
<h1>اضافه کردن ربات جدید</h1>
<form action="/login" method="GET">
<input type="text" placeholder="name" class="nm" name="nm"><br>
<br>
<input type="text" placeholder="token" class="am" name="am"><br>
<br>
<input type="submit" value="submit">
</div>
<div class="delbot">
<h1>حذف ربات</h1>
<form action="/login" method="GET">
<input type="text" placeholder="name" class="nmd" name="nmd"><br>
<br>
<input type="text" placeholder="token" class="amd" name="amd"><br>
<br>
<input type="submit" value="submit">
</div>
</div>
<div class="botstatus">
<h1>وضعیت ربات ها</h1>
<span class="lists">
{{ botstatus }}
</div>
</span>
<div class="sendmsg"></div>
<div class="blockuser"></div>
</div>
</body>
</html>
from flask import *
import Botregistry
this is an external library i've created and uses for create simple data base (i gonna say it in buttom of page) for read bot names with Botregistry.listbots() and add them with Botregistry.addbot(botname , bottoken)
@app.route('/' )
def home():
botss=Botregistry.listbots()
botss = Botregistry.listbots() =['bot1' , 'bot2' , ...]
return render_template('home.html' , botstatus = botss )
my problem here!! 1.how can i create an system to get them and put it in list 2.why when i import html element with flask in {{ example element }} flask I see this :/ :
and my hole init.py:
from flask import *
import Botregistry
app = Flask(__name__)
@app.route('/' )
def home():
botss=Botregistry.listbots()
return render_template('home.html' , botstatus = botss )
@app.route("/login" , methods = ['GET'])
def hello():
if request.method == 'POST':
name = request.form['nm']
token = request.form['am']
Botregistry.addbot(name=name , token=token)
return 'ADDED SUCCESS WITH NAME {name} AND TOKEN {token}'.format(name = name , token = token)
else:
name = request.args.get('nm')
token = request.args.get('am')
Botregistry.addbot(name=name , token=token)
return 'ADDED SUCCESS WITH NAME {name} AND TOKEN {token}'.format(name = name , token = token)
app.run(port=80 , debug=True)
and in the end ... some one knows about an program / system or anything to manage my bots with out need to change they code or create handler in telegram?
Solution 1:[1]
Do I understand your question correctly by saying that you want to display a Python list in your html page? I think you can try to replace
<span class="lists">
{{ botstatus }}
by something like this (looping over each entry to add them to an html list using a jinja for loop):
<ul>
{% for bot in botstatus %}
<li>{{bot}}</li>
{% endfor %}
</ul>
That way, you should be able to get the string values out of your list object.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Steven Robyns |