Trying to use pandas and xlsx writer in GAE - python

So I have created a basic local page on GAE which has a header, a field, and a submit button. My end goal is to create a local page which will take an input of a dataframe from pandas in the field, and convert that to an excel file when I hit the submit button. I have a program written in python already to do this, but I can't figure out how to transfer that over. Here is my code for the page I have already:
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
myPage = """
<html>
<body>
<form action="/hello" method="get">
<div>
<input type="text" name="yourname" size="30" maxlength="30"></input>
</div>
<div>
<input type="submit" value="Submit dataframe">
</div>
</form>
</body>
</html>
"""
self.response.out.write(myPage)
class hello(webapp2.RequestHandler):
def get(self):
yourname = self.request.get('yourname')
self.response.out.write(yourname + " to you too")
application = webapp2.WSGIApplication([
('/', MainPage),('/hello', hello)], debug=True)
When I try to import xlsxwriter or pandas my page won't load. I tried to update my yaml file, but to no avail. I really need to figure out how to do this. It seems like it should be fairly easy, but I don't know where to go from here. In my mind I should be able to just:
import xlsxwriter, pandas, os, re
copy and paste in my other code which only uses those things
make the call: custom_tabs(yourname), which will take in the dataframe and spit out the excel doc.

Related

Grabbing the value of a HTML form input field?

There's a web page similar to: www.example.com/form.php
I want to use Python to grab one of the values from the HTML form on the page. For example, if the form had I could get the value "test" returned
I have googled this extensively but most relate to posting form data, or have advice to use Django or cgi-bin. I don't have access to the server directly so I can't do that.
I thought the library REQUESTS could do it but I can't see it in the documentation.
HTML:
<html>
<body>
<form method="" action="formpost.php" name="form1" id="form1">
<input type="text" name"field1" value="this is field1">
<input type="hidden" name="key" value="secret key field">
</form>
</body>
As an example, I'd like something like this in Python:
import special_library
html = special_library.get("http://www.example.com/form.php")
print html.get_field("wanted")
Has anyone got any suggestions to achieve this? Or any libraries I may not have thought of or been aware of?
You can use requests library, and lxml
Try this:
import requests
from lxml import html
s = requests.Session()
resp = s.get("http://www.example.com/form.php")
doc = html.fromstring(resp.text)
wanted_value = doc.xpath("//input[#class='wanted_class_name']/#value")
print(wanted_value)
You can check following resources:
requests
xpath

How to Execute Python Code on Server from an HTML Form Button

I want to execute python code when user clicks an HTML Form Button. How is it going to be possible? Is it possible that users are not able to view the python code on the server? The forms input are going to be variables in the python code. I am using Flask framework and python 2.7. Security is not a concern yet.
my route.py file:
from pytube import YouTube
from moviepy.editor import *
import os
app = Flask(__name__)
#app.route("/",methods=['GET','POST'])
def landing():
return render_template("index.html",ytdir=ytdir,ytlink=ytlink)
if __name__ == "__main__":
app.run(debug=True)
The index.html in the Template folder:
<form method="POST" action="/">
<input type="text" class="form-control" name="ytdir" placeholder="Example: C:\Downloads">
<h4>The YouTube Video that you want to download</h4>
<textarea class="form-control" rows="3" name="ytlink" placeholder="Example: https://www.youtube.com/watch?v=HipaBLsGB_Y"></textarea>
</form>
<input class="btn btn-primary" type="submit" value="Download">
basically, I have a python code that download the YouTube video into the local drive by using ytdir and ytlink.
Where should I place the python code so that it is executed when the button is clicked.
I recommend for you to do an "api call"
#app.route("/ytdl", methods=["GET"])
def download_video(link):
# call your download code here
# and return the video as an input stream
# read the flask tutorial on how to do that
In the javascript use axios or something to download the file using jquery
like $button.click(() => axios.get(http://<localhost url>:<port>/ytdl) go and read the doc again how to do ajax call

Pass HTML input to python script

I have a Python script and I want to create a very simple HTML form with 3 fields (username, password and an ID) and a submit button.
When I click Submit I just want to pass these three parameters into my Python script and run the script.
I tried to do it using CGI. I created a cgi-bin folder and added my test.py file in there. The Python code is the following:
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
username = form.getvalue('username')
password = form.getvalue('password')
room_id = form.getvalue('room_id')
#More code here...
Then I created an index.html file:
<!DOCTYPE html>
<html>
<body>
<form name="search" action="/[path to file]/test.py" method="get">
Username: <input type="text" name="username"> <br>
Password: <input type="password" name="password"> <br>
Room ID: <input type="text" name="room_id"> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Finally, I added a server.py file:
import CGIHTTPServer
CGIHTTPServer.test()
I run the server.py file and go to 0.0.0.0:8000. I gave the input but when I click Submit the python code is printed in the form and it is not executed.
I am looking for the simplest and fastest way to make this HTML form execute the script. It is only for test purposes and I do not want to spend time creating a complex web application.
Is CGI a good idea? If yes, what am I doing wrong?

Parse HTML string from a file and remove element using xpath and write it to same file in python

For my project, I have to remove selective content from a html file using python xpath.
The selective element can be removed using .remove() method, however the content in file looks same.
How do I write the modified content to that file again?
Though If i try to write the same tree object to file using open().write(etree.tostring(tree_obj)), will the content differs for unicode pages? Is there anyother way to keep the modified file?
Why the header tags in below output has different value after printing the tree object?
Please suggest.
Below is my code sample.
Example: I need to remove all div tags inside the html page.
HTML file:
<html>
<head>test</head>
<body>
<p>welcome to the world</p>
<div id="one">
<p>one</p>
<div id="one1">one1</div>
<ul>
<li>ones</li>
<li>twos</li>
<li>threes</li>
</ul>
</div>
<div id="hell">
<p>heaven</p>
<div id="one1">one1</div>
<ul>
<li>ones</li>
<li>twos</li>
<li>threes</li>
</ul>
</div>
<input type="text" placeholder="enter something.." />
<input type="button" value="click" />
</body>
</html>
Python file:
# _*_ coding:utf-8 _*_
import os
import sys
import traceback
import datetime
from lxml import etree, html
import shutil
def process():
fd=open("D:\\hello.html")
tree = html.fromstring(fd.read())
remove_tag = '//div'
for element in tree.xpath(remove_tag):
element.getparent().remove(element)
print etree.tostring(tree)
process()
OUTPUT:
<html>
<head/><body><p>test
</p>
<p>welcome to the world</p>
<input type="text" placeholder="enter something.."/>
<input type="button" value="click"/>
</body></html>
I haven't worked on python but i have played with parsing html based websites using Java with help of library jsoup.
Python also has similar one like this. Beautiful soup. You can play with this thing to get desired output.
Hope it helps.
Have you tried using python's standard library re?
import re</br>
re.sub('<.*?>','', '<nb>foobar<aon><mn>')
re.sub('</.*?>','', '</nb>foobar<aon><mn>')
The above two operations could be used in combination to remove all the html tags. It can be easily modified to remove the div tags too.

Displaying results from search API

I'm trying to get to grips with web2py/python. I want to get the user to fill in a search form, the term they search for is sent to my python script which should send the query to the blekko API and output the results to them in a new HTML page. I've implemented the following code but instead of my normal index page appearing, I'm getting the html response directly from blekko with '%(query)' /html appearing in it's search bar. Really need some help with this!
HTML form on the default/index.html page
<body>
<div id="MainArea">
<p align="center">MY SEARCH ENGINE</p>
<form name="form1" method="get" action="">
<label for="SearchBar"></label>
<div align="center">
<input name="SearchBar" type="text" id="SearchBar" value="" size = "100px"><br />
<input name="submit" type="submit" value="Search">
</div>
</form>
<p align="center"> </p>
Python code on the default.py controller
import urllib2
def index():
import urllib2
address = "http://www.blekko.com/?q='%(query)'+/html&auth=<mykey>"
query = request.vars.query
response = urllib2.urlopen(address)
html=response.read()
return html
I think you are misunderstanding how string formatting works. You need to put the address and query together still:
address = "http://www.blekko.com/?q='%(query)s'+/html&auth=<mykey>" % dict(query=request.vars.query)
Add a hidden field to your form, call it "submitted". Then reformat your controller function as such:
import urllib2
def index():
if request.vars.submitted:
address = "http://www.blekko.com/?q='%(query)'+/html&auth=<mykey>"
query = request.vars.query
response = urllib2.urlopen(address)
html=response.read()
return html
else:
return dict()
This will show your index page unless the form was submitted and the page received the "submitted" form variable.
The /html doesn't do anything. Glad your question got answered. There is python client code for the blekko search api here: https://github.com/sampsyo/python-blekko

Categories