3

I have a website with a form allowing users to upload files ( pdf or docx ). I'm trying to execute some python code on the file loaded and return and print the text of the file on the HTML page.

After some research, I understood that :

  • we call the python script thanks to the html form. This call execute the python script which receives the data throught the post method

  • The python script does his job and return html code with the result.

For the html part :

<html>
<head>
<title></title>
</head>
<body>

  <h1>Upload File</h1>
  <form action="example.py" method="POST" enctype="multipart/form-data">
      File: <input name="file" type="file">
      <input name="submit" type="submit">
  </form> 

</body>
</html>

For the python part :

def convert_doc_to_text(filename):
    text = textract.process(filename) # extract text from file 
    return text.decode('utf-8')
  • With which technology would I be able to host and execute the python script ?
  • How could I transfer the file uploaded between the website server and the python script server ?

Thanks in advance.

Louis
  • 55
  • 1
  • 4
  • 2
    look for web frameworks in Python: Django, Flask, Bottle, etc. Or look for `CGI` scripts (Python, Perl, bash, etc) in web server but it is old technology and less popular. – furas May 23 '19 at 15:24
  • 1
    see [PythonAnywhere.com](https://www.pythonanywhere.com/) hosting. It can use Flask, Django, Bottle, etc. – furas May 23 '19 at 15:27
  • 2
    Short answer, `mod_wsgi`, but there are better methods now, specifically what @furas mentioned. Create a REST service using `flask` and have your JS call the appropriate API function that way, then manipulate whatever result you get back. – dmn May 23 '19 at 15:27
  • https://wiki.python.org/moin/WebFrameworks – PM 77-1 May 23 '19 at 18:01

0 Answers0