Issue
So I'm new to python and even newer to flask. I'm trying towrite the code for a website that will be run on a raspberry pi 4. It will hopefully turn on my PC via wake on LAN when I'm not at home with it.
Trying to run the code I wrote gives me an invalid syntax error. I tried fixing it, but with my limited knowledge I found that it's next to impossible.
The python code
# Import flask and wakeonlan modles
from flask import Flask
from flask import render_template
from wakeonlan import send_magic_packet
# Create flask app
app= Flask(__name__)
# Define the MAC adress of your PC.
PC_MAC = "Mac goes here,but I took it out before uploading to Stack Overflow. Don't worry, I did write this one correctly."
# Define a route for the home page
@app.route("/")
# Render the index.html template
reder_template("index.html")
# Defines a route forthe wake up action
@app.route("/wakeup")
def wakeup():
# Send a wake on lan packet to the PC
send_magic_packet(PC_MAC)
# Return a success messagesss
return "Your PC has been turned on!"
# Run the app
if __name__ == "__main__":
app.return()
The HTML code
<!DOCTYPE html>
<html>
<head>
<title>
Wake on LAN website!
</title>
</head>
<body>
<h1>
Wake on LAN Website.
</h1>
<p1>
This website allows me to turn on my PC remotely using wake on lan.
</p1>
<button
onclick="window.location.href='/wakeup'">
<Turn on PC>
</button>
</body>
</html>
The python file is located on the desktop and is called "app.py". The html file is located in Desktop/templates and is called "index.html".
Please explain to me what I did wrong and how to fix it as if I was a 5 year old, as that is how low my level of understanding python and flask goes.
(And yes, for the website I do have a DNS)
Solution
The reason code is throwing an Invalid Syntax error is because the render_template method needs to be returned from a function, and not called directly. Moreover, to run the app, you have to use app.run()
not app.return()
The following modified code will work
# Import flask and wakeonlan modles
from flask import Flask
from flask import render_template
# Create flask app
app = Flask(__name__)
# Define the MAC adress of your PC.
PC_MAC = "Mac goes here,but I took it out before uploading to Stack Overflow. Don't worry, I did write this one correctly."
# Define a route for the home page
@app.route("/")
# Render the index.html template
def homepage():
return render_template("index.html")
# Defines a route forthe wake up action
@app.route("/wakeup")
def wakeup():
# Send a wake on lan packet to the PC
# Return a success messagesss
return "Your PC has been turned on!"
# Run the app
if __name__ == "__main__":
app.run()
Answered By - shravan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.