Home>

I want to output two sets of image list in html.
I want to output in table format because it outputs multiple, so I am trying to create a table in html format with the function create_table of index.py below and rewrite --table--.

In translationThe execution environment is offline and its html is displayed locally.

I am trying to set up a cgi server, update the html table with python and display it on localhost.

Folder structure (I'm sorry it's hard to see.)
root
-cgi-bin
--index.py
-cgiserver.py
-res
--test1
---a.png
---b.png
--test2
---a.png
---b.png

problem

Creating a list in a loop doesn't work, but if I copy the output from the terminal and hardcode the list it works.

I also confirmed that there is no difference between the list created in the loop (table_data1) and the hard-coded list (table_data2), but it returned True.

Sorry to trouble you, but I would appreciate if you could point out what is wrong.
Thank you.

Applicable source code

cgiserver.py

import http.server
import subprocess
http.server.test(HandlerClass=http.server.CGIHTTPRequestHandler)
subprocess.call('python -m http.server --cgi')


index.py

import glob
import pathlib
print('Content-type: text/html;charset=UTF-8\r\n')

html = """
<!DOCTYPE html>
<html>
  <meta charset="utf-8">
  <head>
      <title>Images List</title>
  </head>
  <body>
        aaa
        Images
        --table--
  </body>
</html>
"""

def create_table(table_data):
    rows =''
    for case in table_data:
        columns =''
        for cell in case:
            columns += "\t\t\t\t<td></td>\n".format(cell)
        rows += "\t\t\t<tr>\n{}\t\t\t</tr>\n".format(columns)
    table = "<table>\n{}\n\t\t</table>".format(rows)
    return table
test1_dir ='../res/test1/'
test2_dir ='../res/test2/'
test1_images = sorted([pathlib.Path(f).name for f in glob.glob(test1_dir +'*.png')])
test2_images = sorted([pathlib.Path(f).name for f in glob.glob(test2_dir +'*.png')])
Extract only the files in #test1 and test2.
to_be_listed = set(test1_images) | set(test2_images)
table_data1 = [[test1_dir + case_name, test2_dir + case_name] for case_name in to_be_listed]
table_data2 = [['../res/test1/a.png','../res/test2/a.png'], ['../res/test1/b.png','../res /test2/b.png']]
table_html = create_table(table_data1)
html = html.replace('--table--', table_html)
# display the html
print(html)
  • Answer # 1

    test1_dir ='../res/test1/'
    test2_dir ='../res/test2/'
    test1_images = sorted([pathlib.Path(f).name for f in glob.glob(test1_dir +'*.png')])
    test2_images = sorted([pathlib.Path(f).name for f in glob.glob(test2_dir +'*.png')])

    When doing glob, I specified the file with a relative path,
    The reference position of this relative path is the position where the python command is executed

    /rootsocgiserver.pyIf you run
    The reference position of the relative path is/rootMay be,
    In that case the file cannot glob

    at least,/root/cgi-binsoindex.pyIf you run
    You will get the following result:

    $cd /root/cgi-bin
    $python index.py
    Content-type: text/html;charset=UTF-8
    <!DOCTYPE html>
    <html>
      <meta charset="utf-8">
      <head>
          <title>Images List</title>
      </head>
      <body>
            aaa
            Images
          <table>
                          <tr>
                                  <td></td>
                                  <td></td>
                          </tr>
                          <tr>
                                  <td></td>
                                  <td></td>
                          </tr>
                  </table>
      </body>
    </html>

  • Answer # 2

    It was a very elementary mistake.

    Since cgiserver.py is started as root and the cgi server is started, the current directory is root, so glob is a relative path from root.

    Since html is in root/cgi-bin/, you need to fix the relative path. (This was missing.)

    important point

    It is necessary to write it with a relative path because it is said that the method of writing with an absolute path and that local files cannot be accessed.
    (If you access outside the document root, you need to take measures accordingly.)