0

So here's what i'm trying to do:

  • Static Website in HTML(just a simple table) Screenshot Here
  • Admin Page to update text's on that website by html for (I'm not sure if it's good idea) Another Screenshot

I was trying in Python Flask, but I have no idea how to storage data from HTML Form into Python variables. All I did is to use {{ }} in my index.html(first ss) and then I can edit content straight from Python Code, but it's not what I want.

What i want? I want to make a form or smth like that which allows user to easy update text on index(first ss). User has to choose which cell he wants to update and write a new content, then submit and that's all.

Is it Python Flask good idea to do it? (I'm pretty new in Python) At the moment I'm using Wordpress+PostGrid plugin to do something like that, but editing the grid(first ss) is there so hard. I'm stubborn that's why I want to do it on my self.

Any advises about method which I should use or language will be very helpful.

Thanks!

gwenzek
  • 2,816
  • 21
  • 21
Johnatix
  • 21
  • 4

1 Answers1

0

Since you do NOT want to render a template of your table website, but change the HTML file itself, I have the following proposition for you:

Set up a Flask application to receive your request, e.g. on an update route (/update).

Have your admin website use a form that posts to your update route. Make sure to give your select and input tags the name attribute. For example:

<form action="{{url_for('update')}}" method="post">
  <select name="element">
    <option value="imptitle">imptitle</option>
    <option value="zaktitle">zaktitle</option>
  </select>
  <input type="text" name="new_value"> 
  <input type="submit" value="Update">
</form>

In your Flask app you can extract the information from the request by using request.form, using the same names that you specified in your select and input tags, e.g.:

@app.route('/update', methods=['POST'])
def update():
    element = request.form['element']
    new_value = request.form['new_value']

Then replace the element's value within your table HTML: open your table HTML file, read its content, search the element and replace its value using regular expression (you can read up on how to do that here), overwrite the contents of the file with updated content. For example:

with open('index.html', 'r') as f:
    content = f.read()

# new_content = ... (do your search and replace here)

with open('index.html', 'w') as f:
    f.write(new_content)
Elisabeth Strunk
  • 516
  • 1
  • 6
  • 15
  • Hi, thanks so much for your help, I did it this way: – Johnatix Apr 07 '20 at 15:39
  • Did it already :) But I have another question, is it possible if I choose from List (for example "imptitle1") to prepare area where will be display what actualy is printed there? – Johnatix Apr 07 '20 at 16:58
  • I am not sure if I understand your question correctly. Do you want to display a default value in your form in the browser? – Elisabeth Strunk Apr 07 '20 at 17:33
  • No, no, no. I marked cell values with `VALUE`. When I'm on admin page and i choose Imptitle1 from list I'd like to display current value for – Johnatix Apr 07 '20 at 17:37
  • You could make another route that returns the current value of the element, if a GET request is sent; determine the current value by reading the table HTML file and searching via regex again. Of course your front-end would have to send the request and handle the response accordingly. – Elisabeth Strunk Apr 07 '20 at 17:45