I have a university assignment which is to send and receive data via json on a python script via a server and to then display this in a browser with a add and search field. I am adding a students name, surname and age to the dictionary. Please accept my apologises as I'm not the best when it comes to coding.
Currently I can send the information inputted to the received script and it shows as a python dictionary on the python script. I know need to look at getting this to display in a browser e.g chrome with a fuction that can add new students but also search the data dictionary.
Im really struggling how to get the data dictionary to display in a browser. currently it shows on the received script and I can out put with a .txt file with the information.
I'm probably describing this in a rubbish way but any help would be great.
Tired to export as html instead of txt , but I can't find a way of formatting the data and adding a search function. Ive added the data dictionary part below and where it out puts the data to the receive file and a .txt file.
student[fname +" " + sname] = {#assign data to dictionary
'Student First name':fname,
'Student Last name':sname,
'Student Age':age,
'pass':passed
}
go = input("\n press x to exit OR any key to continue")
if go in ["x","X"]:
print ("\n data being sent")
jsonFile = json.dumps(student)#create json file from your dictionary
s.send(jsonFile.encode('utf-8'))
thing = False
print ("\n data sent")
time.sleep(5)
with open('student.txt', 'w') as json_file:
json.dump(student, json_file)
make a html template to take arguments and display the data. like this one.
Add the fields to search and manage the search yourself.
<html>
<title>User Data</title>
<body>
<head>Your head</head>
<p>
Available Student Data in the Database
<table>
<tr>
<td> fname</td>
<td> sname</td>
<td> age</td>
<td> passed</td>
</tr>
{% for user in users %}
<tr>
<td> {{user.fname}}</td>
<td> {{user.sname}}</td>
<td> {{user.age}}</td>
<td> {{user.passed}}</td>
</tr>
{% endfor %}
</table>
</p>
</body>
</html>
then render this html using render_template function of flask library
like this
render_template('user_data.html',users=your_user_data)
make sure that your_user_data is list of students with the specified attributes as written in html template.
As you say :
send and receive data via json on a python script via a server
So basically, you miss the server part. For your case, you'll need a Python web server that you can then connect to your Python script.
Have a look on different Python web server by yourself ;)
For a little projet like that, i recommend you Flask, but that's my opinion.
For sure, don't use Django for that.
Related
I'm learning to create an Omegle bot, but the Omegle interface was created in HTML and I don't know very much about HTML nor MechanicalSoup.
In the part where the text is inserted, the code snippet is as follows:
<td class="chatmsgcell">
<div class="chatmsgwrapper">
<textarea class="chatmsg " cols="80" rows="3"></textarea>
</div>
</td>
In the part of the button to send the text, the code snippet is:
<td class="sendbthcell">
<div class="sendbtnwrapper">
<button class="sendbtn">Send<div class="btnkbshortcut">Enter</div></button>
</div>
</td>
I want to set a text in textarea and send it via button.
Looking at some examples in HTML, I guess the correct way to set text in a textarea is as follows:
<textarea>Here's a text.</textarea>
Also, I'm new at MechanicalSoup, but I think I know how to find and set a value in an HTML code:
# example in the Twitter interface
login_form = login_page.soup.find("form", {"class": "signin"})
LOGIN = "yourlogin"
login_form.find("input", {"name": "session[username_or_email]"})["value"] = LOGIN
From what I understand, the first argument is the name of the tag and a second argument is a dictionary whose first element is the name of the attribute and the second element is the value of the attribute.
But the tag textarea don't have an attribute for setting a text, like value="Here's a text.". What I should do for set a text in a textarea using MechanicalSoup?
I know it's not the answer you expect, but reading the doc would help ;-).
The full documentation is available at:
https://mechanicalsoup.readthedocs.io/
You probably want to start with the tutorial:
https://mechanicalsoup.readthedocs.io/en/stable/tutorial.html
In short, you need to select the form you want to fill-in:
browser.select_form('form[action="/post"]')
Then, filling-in fields is as simple as
browser["custname"] = "Me"
browser["custtel"] = "00 00 0001"
browser["custemail"] = "nobody#example.com"
browser["comments"] = "This pizza looks really good :-)"
I have a small .py program, rendering 2 HTML pages. One of those HTML pages has a form in it. A basic form requesting a name, and a comment. I can not figure out how to take the name and the comment from the form and store it into the csv file. I have got the coding so that the very little I already manually input into the csv file is printed/returned on the HTML page, which is one of the goals. But I can't get the data I input into the form into the csv file, then back n the HTML page. I feel like this is a simple fix, but the Flask book makes absolutely no sense to me, I'm dyslexic and I find it impossible to make sense of the examples and the written explanations.
This is the code I have for reading the csv back onto the page;
#app.route('/guestbook')
def guestbook():
with open('nameList.csv','r') as inFile:
reader=csv.reader(inFile)
names=[row for row in reader]
return render_template('guestbook.html',names=names[1:])
And this is my form coding;
<h3 class="tab">Feel free to enter your comments below</h3>
<br />
<br />
<form action="" method="get" enctype="text/plain" name="Comments Form">
<input id="namebox" type="text" maxlength="45" size="32" placeholder="Name"
class="tab"/>
<br />
<textarea id="txt1" class="textbox tab" rows="6" placeholder="Your comment"
class="tab" cols="28"></textarea>
<br />
<button class="menuitem tab" onclick="clearComment()" class="tab">Clear
comment</button>
<button class="menuitem" onclick="saveComment()" class="tab">Save comment</button>
<br>
</div>
By what I understand all you need is to save the data into the file and you don't know how to handle this in Flask, I'll try to explain it with code as clear as possible:
# request is a part of Flask's HTTP requests
from flask import request
import csv
# methods is an array that's used in Flask which requests' methods are
# allowed to be performed in this route.
#app.route('/save-comment', methods=['POST'])
def save_comment():
# This is to make sure the HTTP method is POST and not any other
if request.method == 'POST':
# request.form is a dictionary that contains the form sent through
# the HTTP request. This work by getting the name="xxx" attribute of
# the html form field. So, if you want to get the name, your input
# should be something like this: <input type="text" name="name" />.
name = request.form['name']
comment = request.form['comment']
# This array is the fields your csv file has and in the following code
# you'll see how it will be used. Change it to your actual csv's fields.
fieldnames = ['name', 'comment']
# We repeat the same step as the reading, but with "w" to indicate
# the file is going to be written.
with open('nameList.csv','w') as inFile:
# DictWriter will help you write the file easily by treating the
# csv as a python's class and will allow you to work with
# dictionaries instead of having to add the csv manually.
writer = csv.DictWriter(inFile, fieldnames=fieldnames)
# writerow() will write a row in your csv file
writer.writerow({'name': name, 'comment': comment})
# And you return a text or a template, but if you don't return anything
# this code will never work.
return 'Thanks for your input!'
I am trying to write some python code to automate the querying of an online medical calculation tool. The ressource is available at:
http://www.shef.ac.uk/FRAX/tool.aspx?lang=en
I am new to this type of thing, but understand from my research that I should be able to use the python requests package for this.
From my inspection of the page source I have identified the form element
<form method="post" action="tool.aspx?lang=en" id="form1">
And the elements that seem to directly correspond to the fields (eg. age) look like this
<input name="ctl00$ContentPlaceHolder1$toolage" type="text" id="ContentPlaceHolder1_toolage" maxlength="5" size="3" onkeypress="numericValidate(event)" style="width:40px;" />
My testing code so far looks like this (The only required fields to have filled out are age, sex, weight and height):
import requests
url="http://www.shef.ac.uk/FRAX/tool.aspx?lang=en"
payload ={'ctl00$ContentPlaceHolder1$toolage':'60',
'ctl00$ContentPlaceHolder1$year':'1954',
'ctl00$ContentPlaceHolder1$month':'01',
'ctl00$ContentPlaceHolder1$day':'01',
'ctl00$ContentPlaceHolder1$sex':'female',
'ctl00$ContentPlaceHolder1$weight':'70',
'ctl00$ContentPlaceHolder1$ht':'165',
'ctl00$ContentPlaceHolder1$facture':'no',
'ctl00$ContentPlaceHolder1facture_hip$':'no',
'ctl00$ContentPlaceHolder1$smoking':'no',
'ctl00$ContentPlaceHolder1$glu':'no',
'ctl00$ContentPlaceHolder1$rhe_art':'no',
'ctl00$ContentPlaceHolder1$sec_ost':'no',
'ctl00$ContentPlaceHolder1$alcohol':'no',
'ctl00$ContentPlaceHolder1$bmd_input':'',
'ctl00$ContentPlaceHolder1$btnCalculate':'Calculate',
}
req = requests.post(url, params=payload)
with open("requests_results.html", "w") as f:
f.write(req.content)
This however does not work. I don't get en error message but the resulting saved html page (which I would later parse for the results) contains just the initial page with no resulting values. In addition to the fields in my current payload the form also contain other elements that are perhaps necessary, such as hidden elements for some of the same data types like age
<input name="ctl00$ContentPlaceHolder1$toolagehidden" type="hidden" id="ContentPlaceHolder1_toolagehidden"
I have tried different combinations of payloads, but the results are the same. Any help would be much appreciated
You want to encode the payload before the POST. Like this:
import urllib
usefulpayload = urllib.urlencode(payload)
Then use usefulpayload in your request.
I'm trying to automate the login to a site, http://www.tthfanfic.org/login.php.
The problem I am having is that the password field has a name that is randomly generated, I have tried using it's label, type and id all of which remain static but to no avail.
Here is the HTML of the form:
<tr>
<th><label for="urealname">User Name</label></th>
<td><input type='text' id='urealname' name='urealname' value=''/> NOTE: Your user name may not be the same as your pen name.</td>
</tr>
<tr>
<th><label for="password">Password</label></th><td><input type='password' id='password' name='e008565a17664e26ac8c0e13af71a6d2'/></td>
</tr>
<tr>
<th>Remember Me</th><td><input type='checkbox' id='remember' name='remember'/>
<label for="remember">Log me in automatically for two weeks on this computer using a cookie. </label> Do not select this option if this is a public computer, or you have an evil sibling.</td>
</tr>
<tr>
<td colspan='2' style="text-align:center">
<input type='submit' value='Login' name='loginsubmit'/>
</td>
</tr>
I've tried to format that for readability but it still looks bad, consider checking the code on the supplied page.
Here is the code I get when printing the form through mechanize:
<POST http://www.tthfanfic.org/login.php application/x-www-form-urlencoded
<HiddenControl(ctkn=a40e5ff08d51a874d0d7b59173bf3d483142d2dde56889d35dd6914de92f2f2a) (readonly)>
<TextControl(urealname=)>
<PasswordControl(986f996e16074151964c247608da4aa6=)>
<CheckboxControl(remember=[on])>
<SubmitControl(loginsubmit=Login) (readonly)>>
The number sequence in the PasswordControl is the part that changes each time I reload the page, in the HTML from the site it seems to have several other tags ascribed to it but none of them work when I try to select them, that or I'm doing it incorrectly.
Here is the code I am using to try and select the control by label:
fieldTwo = br.form.find_control(label='password')
br[fieldOne] = identifier
br[fieldTwo] = password
I can post the rest of my login code if neccesary but this is the only part that is not working, I have had success with other sites where the password name remains the same.
So, is it possible for me to select the passwordControl using it's label, type or ID, or do I need to scrape its name?
EDIT: Oops, forgot to add the error message:
raise ControlNotFoundError("no control matching "+description)
mechanize._form.ControlNotFoundError: no control matching label 'password'
SOLVED:
Solution given by a guy on reddit, thanks Bliti.
Working code:
br.select_form(nr=2)
list = []
for f in br.form.controls:
list.append(f.name)
fieldTwo = list[2]
Solution given by a guy on reddit, thanks Bliti.
Working code:
#Select the form you want to use.
br.select_form(nr=2)
list = []
for f in br.form.controls:
#Add the names of each item in br.formcontrols
list.append(f.name)
#Select the correct one from the list.
fieldTwo = list[2]
I want user to enter a sentence then I break up that sentence into a list. I got the html page down but i have trouble passing that sentence to python.
How do I properly send the user input to be processed by python and output it to a new page?
There are many Python web frameworks. For example, to break up a sentence using bottle:
break-sentence.py:
#!/usr/bin/env python
from bottle import request, route, run, view
#route('/', method=['GET', 'POST'])
#view('form_template')
def index():
return dict(parts=request.forms.sentence.split(), # split on whitespace
show_form=request.method=='GET') # show form for get requests
run(host='localhost', port=8080)
And the template file form_template.tpl that is used both to show the form and the sentence parts after processing in Python (see index() function above):
<!DOCTYPE html>
<title>Break up sentence</title>
%if show_form:
<form action="/" method="post">
<label for="sentence">Input a sentence to break up</label>
<input type="text" name="sentence" />
</form>
%else:
Sentence parts:<ol>
%for part in parts:
<li> {{ part }}
%end
</ol>
%end
request.forms.sentence is used in Python to access user input from <input name="sentence"/> field.
To try it you could just download bottle.py and run:
$ python break-sentence.py
Bottle server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
Now you can visit http://localhost:8080/.
Have you tried Google? This page sums up the possibilities, and is one of the first results when googling 'python html'.
As far as I know, the two easiest options for your problem are the following.
1) CGI scripting. You write a python script and configure it as a CGI-script (in case of most HTTP-servers by putting it in the cgi-bin/ folder). Next, you point to this file as the action-attribute of the form-tag in your HTML-file. The python-script will have access to all post-variables (and more), thus being able to process the input and write it as a HTML-file. Have a look at this page for a more extensive description. Googling for tutorials will give you easier step-by-step guides, such as this one.
2) Use Django. This is rather suited for larger projects, but giving it a try on this level may provide you certain insights, and wetting your appetite for future work ;)