0

I am new to Python and doing web programs with Python but it's for a project. I have a simple main.py file

import win32api
from flask import Flask, render_template

app = Flask(__name__)


# Using the below, the popup message appears on the page load of index.html
# 0x00001000 - This makes the popup appear over the browser window
@app.route('/')
def index():
    win32api.MessageBox(0, 'You have just run a python script on the page load!',
                        'Running a Python Script via Javascript', 0x00001000)
    return render_template('main.html')


# Using the below, the popup message appears when the button is clicked on the webpage.
# 0x00001000 - This makes the popup appear over the browser window
@app.route('/test')
def test():
    win32api.MessageBox(0, 'You have just run a python script on the button press!',
                        'Running a Python Script via Javascript', 0x00001000)
    return render_template('main.html')


if __name__ == "__main__":
    app.run(debug=True)

And main.html

<!DOCTYPE HTML>
<html>
    <head>
        <title> Example </title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    </head>
    <body>
        <h1> Ajax Example </h1>
        <button type="button" onclick="example2()">Click Me!</button>
    </body>
    <script>
        function example () {
            $.ajax({
                url: "app.py",
                context: document.body
            })
        }
        function example2 () {
            $.ajax({
            url:"/test",
            context: document.body});
        }
        window.onload = event => {
            example();
        };
    </script>
</html>

When I run main.py, I get the first dialog box but then I get

jinja2.exceptions.TemplateNotFound
jinja2.exceptions.TemplateNotFound: main.html

Traceback (most recent call last)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\Abdulahad\PycharmProjects\Test\main.py", line 13, in index
return render_template('main.html')
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\templating.py", line 148, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\jinja2\environment.py", line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\jinja2\environment.py", line 997, in get_template
return self._load_template(name, globals)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\jinja2\environment.py", line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\jinja2\loaders.py", line 125, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\templating.py", line 59, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\Abdulahad\PycharmProjects\Test\venv\Lib\site-packages\flask\templating.py", line 95, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: main.html
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object

How can I fix this? I really don't understand how the first dialog displayed but not the second one. I am getting an error that my post is mostly code because of the large error, so I am just trying to add more gibberish.

Python441
  • 9
  • 1
  • 1
    I have a folder called Test which has main.html, main.py, venv, and .idea. The two folders have many folders and files – Python441 Mar 21 '22 at 16:33

0 Answers0