moinz
Posts: 5
Joined: Mon Mar 26, 2018 7:14 pm

HTML to edit Python script (for email)

Mon Mar 26, 2018 8:37 pm

I've set up Raspberry Pi 3 with the Picamera, PIR. When motion detected, starts to record. Email is sent too. All is working.

I'm now trying to find a way to edit the python script (the code) in a user-friendly way rather than going directly into the .py script. So for example, if I wanted to change the email address, I could easily do this- maybe in HTML or something?

Below is taken from my script (some omitted)
The parts I would want to change are highlight in bold

myscript.py
----------
....
#sending email

smtpUser = 'myemailaddress@gmail.com'
smtpPass = 'myemailpassword here'

toAdd = 'myemailaddress@gmail.com'
fromAdd = smtpUser

#Subject, header, body
subject = 'subject is here'
header = 'To: ' + toAdd + '\n' + 'From: ' + fromAdd + '\n' + 'Subject: ' + subject
body = 'message here'
s = smtplib.SMTP ('smtp.gmail.com',587)

s.ehlo()
s.starttls()
s.ehlo()
s.login(smtpUser, smtpPass)
s.sendmail (fromAdd, toAdd, header + '\n\n' + body)
s.quit()

--------


Is this possible? Can I create a webpage / form which is linked to specific part of the script and change it? I've looked online and couldn't find anything. Any help is much appreciated!

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: HTML to edit Python script (for email)

Tue Mar 27, 2018 9:13 am

This would generally be considered a bad idea. Hardcoding this kind of information inside code is also considered bad practice.

Some options to consider instead.
1. Store the information in a database (MySQL, SQLite etc etc)
2. Store the information in a text file (consider using JSON or configparser
3. Store it in Environment variables.

Your code can then read the login details from one of these sources.

If you want a web interface to change these things. I'd suggest you look at python-flask, it will allow you to create webpages dynamically with forms, backend access to databases etc etc.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

moinz
Posts: 5
Joined: Mon Mar 26, 2018 7:14 pm

Re: HTML to edit Python script (for email)

Thu Mar 29, 2018 5:23 pm

del;
Last edited by moinz on Thu Mar 29, 2018 5:28 pm, edited 1 time in total.

moinz
Posts: 5
Joined: Mon Mar 26, 2018 7:14 pm

Re: HTML to edit Python script (for email)

Thu Mar 29, 2018 5:28 pm

scotty101 wrote:
Tue Mar 27, 2018 9:13 am
This would generally be considered a bad idea. Hardcoding this kind of information inside code is also considered bad practice.

Some options to consider instead.
1. Store the information in a database (MySQL, SQLite etc etc)
2. Store the information in a text file (consider using JSON or configparser
3. Store it in Environment variables.

Your code can then read the login details from one of these sources.

If you want a web interface to change these things. I'd suggest you look at python-flask, it will allow you to create webpages dynamically with forms, backend access to databases etc etc.
Thanks for your reply
Just to confirm, it's possible to use Python Flask to create a webpage / form where I can link it with the python script to change a specific setting- i.e. user email address?
Or Storing it in Environment variables? I'm pretty new at this, which would be easiest method?- the web interface or storing it in Environment variables? I have no idea how I would do this

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: HTML to edit Python script (for email)

Thu Mar 29, 2018 6:22 pm

moinz wrote:
Thu Mar 29, 2018 5:28 pm
Thanks for your reply
Just to confirm, it's possible to use Python Flask to create a webpage / form where I can link it with the python script to change a specific setting- i.e. user email address?
Flask will let you create a webpage that can store settings in an intermediate format (database/file/environment variable)
moinz wrote:
Thu Mar 29, 2018 5:28 pm
Or Storing it in Environment variables? I'm pretty new at this, which would be easiest method?- the web interface or storing it in Environment variables? I have no idea how I would do this
If you want to change the settings from a web interface, you need to do both.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”