Python Variables | Python In HTML Examples https://pythoninhtmlexamples.com Showing You How You Can Use Python On Your Website Tue, 06 Oct 2020 20:47:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 194754043 Python Project – Online Quiz Management https://pythoninhtmlexamples.com/python-project-online-quiz-management/?utm_source=rss&utm_medium=rss&utm_campaign=python-project-online-quiz-management Fri, 25 Sep 2020 18:36:30 +0000 https://pythoninhtmlexamples.com/?p=187 I am going to be implementing some Python projects with a browser front end (obviously since that’s what this domain is called), I am going to be focusing on the core functionality, not really the aesthetics. You can always use some stylesheets (CSS) to make it look pretty. I am just concerned with the fact […]

The post Python Project – Online Quiz Management first appeared on Python In HTML Examples.]]>
I am going to be implementing some Python projects with a browser front end (obviously since that’s what this domain is called),

I am going to be focusing on the core functionality, not really the aesthetics. You can always use some stylesheets (CSS) to make it look pretty.
I am just concerned with the fact that it works.

The first one is going to show you how to implement an Online Quiz Management System.

(Click the image to see it in action – login: demo, 123)

It will have 2 views – Admin view and End User view:

Admin view

Log inClick here for the code
Add/Edit/Delete new quiz text (Click for code)
-Add questions and answers for the selected quiz

End User view

Log inClick here for the code
-Select the quiz to take
-View the question
-Answer the question
-Tally the result at the end.

Setup

First off we need to setup a MySQL database to hold all the persistent data for our application.

Login to your control panel (mine is on the hostgator webserver) and add a database called “online-quiz”

now add an admin user…

If you need some more information or help, see this post (from a hostmonster perspective).

Now it will show up in the database administration tool (phpMyAdmin)

Now we can start adding tables.

The first will be the users table (tblUsers).

FacebooktwitterredditpinterestlinkedinmailThe post Python Project – Online Quiz Management first appeared on Python In HTML Examples.]]>
187
How To Simply Connect Python To MySQL On Your Site https://pythoninhtmlexamples.com/how-to-simply-connect-python-to-mysql-on-your-site/?utm_source=rss&utm_medium=rss&utm_campaign=how-to-simply-connect-python-to-mysql-on-your-site Mon, 05 Aug 2019 21:44:07 +0000 http://pythoninhtmlexamples.com/?p=66 Nearly all the visitors to this website are interested in how to integrate the python programming language into their website. We are going through a website I originally build in PHP and MySQL, and reconstruct the site using python and MySQL. Interesting. This will give some good insight into python concepts. In this post, I […]

The post How To Simply Connect Python To MySQL On Your Site first appeared on Python In HTML Examples.]]>
Nearly all the visitors to this website are interested in how to
integrate the python programming language into their website.

We are going through a website I originally build in PHP and MySQL, and reconstruct the site using python and MySQL.

Interesting. This will give some good insight into python concepts.

In this post, I am going to show you how to simply connect your Python webpage to the MySQL server on your website.

In this small bit of code I am demonstrating a how you can connect your python webpage to your MySQL server.

#!/usr/bin/python

import MySQLdb

def dbconnect(): 
    conn = MySQLdb.connect('localhost', 'username','password', 'database')

    cursor = conn.cursor()

    sql="SELECT * FROM tblContestants"

    cursor.execute(sql)

    #any print items need to be below this line:                             
    print "Content-type: text/html\n\n";
    
    for row in cursor:
        #this prints out the 1st column
        print(row[0]) 

#start here:
dbconnect()

With Python, you can import “modules”. In this case to connect to the MySQL server I’m importing the “MySQLdb” module. I can choose to use the code in this module without trying to recode all of the MySQL logic, which will probably take me months! Trust me, this is easier :).

Note: All procedures in Python begin with the “def” keyword.

Now that new “dbconnect” procedure can be used each time I need to make a connection to the database.

…and there are curly brackets around your procedures, just make sure you make the proper indenting, or else your code WILL FAIL.

Indenting is very important in Python. To make sure your indenting is correct start out with the “IDLE” text editing program that should have come with your installation of Python. I have also used “Dr. Python” in the past.

def dbconnect(): 

My “cursor” is like my recordset, and I can loop through my recordset like this:

    
        print "

These are contestants from a MySQL table:

" for row in cursor: #this prints out the 1st column print(row[0])

Remember that since we are outputting text to the screen, this needs to be present:

print "Content-type: text/html\n\n";

PS. If running your file gives you a 500 error, make sure you set the file permissions to 755.

Here it is in action:

http://pythoninhtmlexamples.com/files/mysql.py

FacebooktwitterredditpinterestlinkedinmailThe post How To Simply Connect Python To MySQL On Your Site first appeared on Python In HTML Examples.]]>
66
Simple Python Variables Examples https://pythoninhtmlexamples.com/simple-python-variables-examples/?utm_source=rss&utm_medium=rss&utm_campaign=simple-python-variables-examples Fri, 05 Jul 2019 17:36:21 +0000 http://pythoninhtmlexamples.com/?p=49 In this post I am going to show you 2 simple ways of using variables in your python html files. The 1st way is using a text, string, variable #!/usr/bin/python msg='Python Text Variable.' print "Content-type: text/html\n\n"; print "<html><head>"; print "<title>Simple Python Text Variable</title>"; print "</head><body>"; print "<p>" + msg + "</p>"; print "</body></html>"; Here is […]

The post Simple Python Variables Examples first appeared on Python In HTML Examples.]]>
In this post I am going to show you 2 simple ways of using variables in your python html files.

The 1st way is using a text, string, variable

#!/usr/bin/python

msg='Python Text Variable.'

print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>Simple Python Text Variable</title>";
print "</head><body>";
print "<p>" + msg + "</p>";
print "</body></html>";

Here is a working example:

http://pythoninhtmlexamples.com/files/python_text_variable.py

Way number 2 is to use a integer in your html file:

#!/usr/bin/python

n1 = 2
n2 = 4

#here I am casting my variables as an integer (numeric) value so I can calculate them.
total= (int(n1) * int(n2))

print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>Multiplication Of Numbers </title>";
print "</head>";
print "<body>";

#to print out the total variable I have to convert the integer value into a string variable.
print "<p>total= " + str(total) + "</p>";
print "</body>";
print "</html>";

Here is a working example:
http://pythoninhtmlexamples.com/files/simple_math.py

PS. if your file constantly gives you an error, and your syntax is totally correct, and you are about to throw the computer across the room in frustration 🙂 , make sure you set your file permissions to 755.

FacebooktwitterredditpinterestlinkedinmailThe post Simple Python Variables Examples first appeared on Python In HTML Examples.]]>
49