Self processing page with sql and python - python

Im creating a shop page as my college project. I have a database set up and Im trying to use the select and submit to open a new page which will display information about the selected part. I just cant seem to be able to extract data from the database, can you help please ?
That's a part of my html
<p class="heading2">List of CPU's:</p>
<form action="show_part.py">
<select name="component">
<option value="Intel_i7-5960x">Intel i7-5960x</option>
Then this is the python
#!/usr/local/bin/python3
from cgitb import enable
import pymysql as db
from cgi import FieldStorage
print('Content-Type: text/html')
print()
form_data = FieldStorage()
component = form_data.getfirst("name")
result=''
try:
connection = db.connect('***', '***', '***', '***')
cursor = connection.cursor(db.cursors.DictCursor)
result += cursor.execute("""SELECT price
FROM components
WHERE name = %s""" % (component))
cursor.close()
connection.close()
except db.Error:
result = '<p>Sorry! We are experiencing problems at the moment. Please call back later.</p>'
print("""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Part details</title>
</head>
<body>
%s
</body>
</html>""" % (result))
What happens is I keep getting the error message in the new page, meaning the code is fine, just something about the database query. Any suggestions ?

I think I cracked it, problem was in the sql query, where the program tried to execute:
SELECT price
FROM components
WHERE name=intel_i7-5960x
what is missing is the quotes:
SELECT price
FROM components
WHERE name='intel_i7-5960x'
so I changed that in my html file and it gets me not exactly where I want to go, but its a start !

Related

How to see the output after getting the data in web scraping?

I typed some code to get some data from a site (scraping) and I got the result I want (results are just numbers).
the question is, how can I show results as an output on my website? I am using python and HTML in Vs Code. Here is the scraping code:
import requests
from bs4 import BeautifulSoup
getpage= requests.get('https://www.worldometers.info/coronavirus/country/austria/')
getpage_soup= BeautifulSoup(getpage.text, 'html.parser')
Get_Total_Deaths_Recoverd_Cases= getpage_soup.findAll('div', {'class':'maincounter-number'})
for para in Get_Total_Deaths_Recoverd_Cases:
print (para.text)
Get_Active_And_Closed= BeautifulSoup(getpage.text, 'html.parser')
All_Numbers2= Get_Active_And_Closed.findAll('div', {'class':'number-table-main'})
for para2 in All_Numbers2:
print (para2.text)
and these are the results that I want to show on the website:
577,007
9,687
535,798
31,522
545,485
I don't know how to describe this solution but it is a solution nonetheless:
import os
lst = [
577.007, 9.687, 535.798, 31.522, 545.485
]
p = ''
for num in lst:
p += f'<p>{num}</p>\n'
html = f"""
<!doctype>
<html>
<head>
<title>Test</title>
</head>
<body>
{p}
</body>
</html>
"""
with open('web.html', 'w') as file:
file.write(html)
os.startfile('web.html')
You don't have to necessarily use that os.startfile but You get the idea I hope, You now have web.html file and You can display it on Your website or whatever and theoretically You can also get a normal html document and copy it there (set as html variable and put the {p} wherever You need/want) or even go to Your html and put somewhere for example {%%} (which is what django uses but anyways),then read the whole file and replace those {%%} with Your value and write that back.
At the end this is a solution but depending on what You use e.g. a framework of sorts for example django or flask, there are easier ways to do that there

Jenkins job to fetch data from database and display in jenkins

On a higher level -
Using Jenkins (by creating a Job) is it possible to fetch data from database and display extracted data in that job or attach as artifact for that job ?
One options which was considered -
Create a job with build step being 'execution of Python script'. This scripts connects to database and fetch data.
c = sqlite3.connect('db_path.db')
cur = c.cursor()
cur.execute("SELECT column_name from table_name LIMIT 11")
test = cur.fetchall()
And dump the results in html format using a template or something like that
<table>
{% for row in test %}
<tr>
<th> {{ row[0] }}</th>
</tr>
{% endfor %}
</table>
But the question here is, how do I display this html file in Jenkins job or attach as artifact, considering this job will be used multiple times for various jobs.
I could use HTML Publisher Plugin, but the limitation is that the in my case the html name cannot be static. And I don't think this will work in my case and I don't know how to link html to python fetchall().
Can anyone please help me with this. Is there any other/better option to perform this activity ?
Question 1: How could I establish connection from fetchAll() to the html page ?
Question 2: I saw in HTML Publisher Plugin that index.html is the html page used to publish. Will this page be a standard template ? or everytime I run a job with different configurations will this page be different ?
I don't know how to do it !!! :(
You can archive the html report for each run even as it runs concurrently. If the job is dynamic you can also add a description for the job so each run will make sense to you using the Description plugin. In the job configuration you can define how many instances you'd like to keep, how many with artifacts, etc.
In general your process sounds ok for what you want to do, where is the limitation?
To fill data into HTML in Python you can use Mako.Templates.
Quick example:
from mako.template import Template
from mako.lookup import TemplateLookup
from mako.runtime import Context
def fill_template(template, data):
lookup = TemplateLookup(directories=["templates"])
mytemplate = Template(filename=template, lookup=lookup)
buf = io.StringIO()
ctx = Context(buf, data=data)
mytemplate.render_context(ctx)
htm = buf.getvalue()
print(htm)
file = open("Report/index.html", "wb")
file.write(bytes(htm.encode('utf-8')))
file.close()
return 0
fdata = {'test':data}
fill_template("templates/digest.html", fdata)
Template file:
<table style="border-bottom:solid black 1pt; cellspacing:5%">
% for test in sorted(data['test']):
<tr>
.... Do something with test data (depends on how you store it) ...
</tr>
% endfor
</table>

Bottle Displays Old Template?

I made a first draft of a template, called batch.tpl. I have updated it, however, the old template still displays. I have shut off the controller script and turned it back on multiple times. I have removed everything from the template except for the following:
cat views/batch.tpl
<HTML>
<HEAD>
<TITLE> Batch Manager </TITLE>
</HEAD>
<BODY>
THIS ISN'T DISPLAYING IN BROWSER
</BODY>
</HTML>
Yet that will not show, just the old template.
Here is my controller:
cat brew_bottle.py
from bottle import route, run, template, debug, post, request
import MySQLdb
import pymongo
from datetime import datetime, timedelta
import datetime
#route('/batch')
def batch():
con = MySQLdb.connect('localhost', 'root', 'pi', 'brew')
cursor = con.cursor()
batch_sql = "SELECT name, material, start_date, active, end_begin_date, end_end_date, min_temp, max_temp FROM batch WHERE active='Y'"
cursor.execute(batch_sql)
batches = cursor.fetchall()
return template('batch', batches=batches)
#...
debug(True)
run(host='0.0.0.0', port='8080', reloader=True)
However, when I go to http://<hostname>:8080/batch, I see the old template that I had wrote:
I am sure I am missing something easy. What is it?
I created a new directory and moved everything to it. Then when I start the python script, I am able to see the correct page. But the python script in the old directory displays the old template?
According to the Bottle docs, auto reloading (reloading) is only triggered by changes to module files.
Changes in template files will not trigger a reload. Please use debug
mode to deactivate template caching.
Turn on Bottle's debug mode this way:
bottle.debug(True)
Here is an incomplete list of things that change in debug mode:
The default error page shows a traceback. Templates are not cached.
Plugins are applied immediately. Just make sure not to use the debug
mode on a production server.
N.B., I see that someone already asked you whether restarting the server caused the updated template to appear, and that you indicated that it did not. That's highly suspicious. In any case, try fixing the problem above and see whether that solves your problem. Good luck!

Webpage redirect to the main page with CGI Python

As my first web app I developed a very simple survey. Random questions are being asked from the user anytime the page refreshes. The answer is sent to a cgi script using post to save the answers to the database.
However, when the user presses the submit button it automatically goes to the page which is responsible for processing the data and since it doesn't have any output it is a blank page. Now if the user wants to answer another question they have to press the "back" in the browser and refresh the page so a new question pops up. I don't want this.
I want it in a way that when the users pressed submit, the answers go automatically to the processing script and the page refreshes itself with a new question or at least after processing it redirects to the main survey page with a new question.
You want to implement this: https://en.wikipedia.org/wiki/Post/Redirect/Get
It's much simpler than it sounds. The CGI script that receives the POST must simply produce the following output:
Status: 303 See other
Location: http://lalala.com/themainpage
You can also send a HTTP header from your processing script:
Location: /
After you have processed your answer, you would send the above header. I would recommend you append a random number query string. e.g. python example (assuming you're using the python CGI module):
#!/usr/bin/env python
import cgitb
import random
import YourFormProcessor
cgitb.enable() # Will catch tracebacks and errors for you. Comment it out if you no-longer need it.
if __name__ == '__main__':
YourFormProcessor.Process_Form() # This is your logic to process the form.
redirectURL = "/?r=%s" % random.randint(0,100000000)
print 'Content-Type: text/html'
print 'Location: %s' % redirectURL
print # HTTP says you have to have a blank line between headers and content
print '<html>'
print ' <head>'
print ' <meta http-equiv="refresh" content="0;url=%s" />' % redirectURL
print ' <title>You are going to be redirected</title>'
print ' </head>'
print ' <body>'
print ' Redirecting... Click here if you are not redirected' % redirectURL
print ' </body>'
print '</html>'
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://www.example.com" />
<title>You are going to be redirected</title>
</head>
<body>
Redirecting...
</body>
</html>
See meta-refresh drawbacks and alternatives here.

HTML forms not working with python

I've created a HTML page with forms, which takes a name and password and passes it to a Python Script which is supposed to print the persons name with a welcome message. However, after i POST the values, i'm just getting the Python code displayed in the browser and not the welcome message. I have stored the html file and python file in the cgi-bin folder under Apache 2.2. If i just run a simple hello world python script in the browser, the "Hello World" message is being displayed. I'm using WinXP, Python 2.5, Apache 2.2. the code that i'm trying to run is the following:
#!c:\python25\python.exe
import cgi
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
reshtml = """Content-Type: text/html\n
<html>
<head><title>Security Precaution</title></head>
<body>
"""
print reshtml
User = form['UserName'].value
Pass = form['PassWord'].value
if User == 'Gold' and Pass == 'finger':
print '<big><big>Welcome'
print 'mr. Goldfinger !</big></big><br>'
print '<br>'
else:
print 'Sorry, incorrect user name or password'
print '</body>'
print '</html>'
The answer to it might be very obvious, but its completely escaping me. I'm very new to Python so any help would be greatly appreciated.
Thanks.
This
i'm just getting the Python code
displayed in the browser
sounds like CGI handling with Apache and Python is not configured correctly.
You can narrow the test case by passing UserName and PassWord as GET parameters:
http://example.com/cgi-bin/my-script.py?UserName=Foo&PassWord=bar
What happens if you do this?
You may have to extract the field values like this
User = form.getfirst('UserName')
Pass = form.getfirst('PassWord')
I know, it's strange.

Categories

Resources