Write the output of a script in a html page using flask - python

I wrote a script that makes me able to run a script and then to get the output I want, I am trying to write the output in a html page, how can I do that? this is my script:
def execute(cmd):
os.system(cmd)
to_return =dict()
for filename in files:
with open(filename, 'r') as f:
data = f.read()
to_return[filename] = data
return to_return
output = execute('./script')
print output
Any Idea of how I can generate an html page where I can print the result of running this script??

In your views.py, under the corresponding route, do
#app.route('/route_name')
def script_output():
output = execute('./script')
return render_template('template_name.html',output=output)
And in your template,
<p>{{ output }}</p>

Related

Downloading sites from a list

So, I am a bit new to python, and I can't get my head to wrap around why this code snippet is not working.
In short, I have a list of 500 sites, all in the following format: https://www.domain . com/subfolder/subfolder separated by a new line, and I am trying to download them. This is the code:
import wget
f = open("500_sites.txt", "r")
content = f.readlines()
url = ""
for x in range(1, len(content)):
print(content[x])
wget.download(content[x], 'index.html')
input("wait a bit")
I am expecting the code to read the text file line by line in the content list. Then, I would like the wget.download() function to download the whole source code of the content[x] webpage.
Using the wget.download() with a given variable it works perfectly:
...
url = "https://domain . com/subfolder/subfolder"
wget.download(url, 'index.html')
...
Thanks in advance!

flask ValueError: I/O operation on closed file

I have a code which i am using to scrape from a web page and I am saving the scraped data in a html file and displaying it as a different page. below is the code
from flask import Flask, render_template,request from bs4 import
BeautifulSoup import urllib.request import sys,os app =
Flask(__name__) #app.route('/') def index():
return render_template ('index.html')
#app.route('/result',methods = ['POST']) def result(): if
request.method == 'POST':
result = request.form.get("search")
link = "https://xyz.comindex?search="
url = (link+result)
print(url)
try:
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
test = soup.findAll('div', attrs={"class": "search-inner-wrapper"})
sys.stdout = open("tests.html", "w")
print(test)
sys.stdout.close()
return render_template("SearchResults.html", test=test)
except:
print("An error occured.")
return render_template("test.html", test=test)
if __name__ == '__main__':
app.run(use_reloader = True, debug = True)
My problem is that this code works perfectly fine but just for once, When i reload the index page and perform a search query I get
ValueError: I/O operation on closed file.
I cant figure a work around for this since i have to use single file every time and do not want the results to append with existing code.
You are redefining sys.stdout to be the file handle of the file you opened. Use another name, don't overwrite sys.stdout. And don't close sys.stdout. It's ok to close the file handle you create though.
Example of opening a file and reading it, opening a file and writing it:
bjb#blueeyes:~$ cat /tmp/infile.html
<html>
<head>
</head>
<body>
<div class="search-inner-wrapper">fleeble flobble</div>
</body>
</html>
bjb#blueeyes:~$ cat /tmp/file2.py
#!/usr/bin/env python3
with open('/tmp/infile.html', 'r') as infile:
page = infile.readlines()
with open('/tmp/outfile.html', 'w') as ofd:
ofd.write(''.join(page))
bjb#blueeyes:~$ /tmp/file2.py
bjb#blueeyes:~$ cat /tmp/outfile.html
<html>
<head>
</head>
<body>
<div class="search-inner-wrapper">fleeble flobble</div>
</body>
</html>
The first line of /tmp/file2.py just says this is a python script.
The next two lines open a file called /tmp/infile.html for reading and declare a variable "infile" as the read file descriptor. Then all the lines in /tmp/infile.html are read into a list of strings.
When we leave that "with" block, the file is closed for us.
Then in the next two lines, we open /tmp/outfile.html for writing and we use the variable ofd ("output file descriptor") to hold the file descriptor. We use ofd to write the series of lines in the list "page" to that file. Once we leave that second "with" block, the output file is closed for us. Then the program exits ... my last command dumps out the contents of /tmp/outfile.html, which you can see is the same as infile.html.
If you want to open and close files without using those with blocks, you can:
infile = open('/tmp/infile.html', 'r')
page = infile.readlines()
infile.close()
ofd = open('/tmp/outfile.html', 'w')
ofd.write(''.join(page))
ofd.close()
Hopefully that will work in a flask script ...

How to download a file in Python (Jinja2) on-click Export button?

I have a button export :
<button class="aptButton" formaction="/export/" type="submit">export</button>
and I have this in the /export/
index.cgi
#! /apollo/sbin/envroot $ENVROOT/bin/python
# -*- coding: utf-8 -*-
import cgitb
cgitb.enable()
import cgi
def main():
print "Content-Type: text/html"
print
form = cgi.FieldStorage()
results = helpers.getResults()
environment = helpers.get_environment()
print environment.get_template('export.html').render(
results = results)
main()
and I have this in my export.html
<!doctype html>
{% for id in results %}
{{ write_results_to_file(id) }}
{% endfor %}
I am trying to download the results to a tab separated file, so I thought of writing to a local file and then send(download) the file but I am not sure how to do the download part, I couldnt use flask or django which has some good libs.. is there any other lib which I can use to download the results to a tab delimited file on the users desktop?
export.py
def write_results_to_file(result):
local_filename = "/home/testing.txt"
# NOTE the stream=True parameter
with open(local_filename, 'w') as f:
f.write('\t'.join(result) + '\n')
If you're using good old-fashioned CGI to produce a tab-separated file,
all you need to do is print an appropriate header and then print the content on stdout, something like this:
def main():
form = cgi.FieldStorage()
results = helpers.getResults()
print "Content-Type: text/plain"
print "Content-Disposition: attachment; filename=testing.txt"
print
for result in results:
print '\t'.join(result) + '\n'
main()
The essential parts are the 2 lines that print the header,
followed by a blank line to separate from the content,
followed by the plain text content.
If you want to make this happen on the click of an Export button,
then you can, for example:
Make the Export button a link to another URL endpoint that will use the example script I put above
Or, use the same script, with a conditional statement on form parameters to decide to print the front page, or to print the content using the example script above
Let me know if you need further help.

Python using xhtml2pdf to print webpage into PDF

I am trying to using xhtml2pdf to print webpage into local disk PDF files. There's an example found as below.
It runs and doesn't return error. However it doesn't convert the webpage but only a sentence. in this case, only 'http://www.yahoo.com/' is written into the PDF file.
How can I actually convert the web page into PDF?
from xhtml2pdf import pisa
sourceHtml = 'http://www.yahoo.com/'
outputFilename = "test.pdf"
def convertHtmlToPdf(sourceHtml, outputFilename):
resultFile = open(outputFilename, "w+b")
pisaStatus = pisa.CreatePDF(sourceHtml,resultFile)
resultFile.close()
return pisaStatus.err
if __name__=="__main__":
pisa.showLogging()
convertHtmlToPdf(sourceHtml, outputFilename)
xhmlt2pdf is not going to work with all the websites, for one, it is not working for yahoo.com. But the reason it is not working here is you are not providing the actual HTML file to pisa but rather providing the URL, you want to fetch the HTML first, for example using urllib2:
url=urllib2.urlopen('http://sheldonbrown.com/web_sample1.html')
srchtml=url.read()
pisa.showLogging()
convertHtmlToPdf(srchtml, outputFilename)
And it will work. That is a very simple sample HTML.
thanks to CT Zhu's help. just putting down the workable one, for reference:
from xhtml2pdf import pisa
import urllib2
url=urllib2.urlopen('http://sheldonbrown.com/web_sample1.html')
sourceHtml=url.read()
pisa.showLogging()
outputFilename = "test555.pdf"
def convertHtmlToPdf(sourceHtml, outputFilename):
resultFile = open(outputFilename, "w+b")
pisaStatus = pisa.CreatePDF(sourceHtml,resultFile)
resultFile.close()
return pisaStatus.err
if __name__=="__main__":
pisa.showLogging()
convertHtmlToPdf(sourceHtml, outputFilename)

Loading mako templates from files

I'm new to python and currently trying to use mako templating.
I want to be able to take an html file and add a template to it from another html file.
Let's say I got this index.html file:
<html>
<head>
<title>Hello</title>
</head>
<body>
<p>Hello, ${name}!</p>
</body>
</html>
and this name.html file:
world
(yes, it just has the word world inside).
I want the ${name} in index.html to be replaced with the content of the name.html file.
I've been able to do this without the name.html file, by stating in the render method what name is, using the following code:
#route(':filename')
def static_file(filename):
mylookup = TemplateLookup(directories=['html'])
mytemplate = mylookup.get_template('hello/index.html')
return mytemplate.render(name='world')
This is obviously not useful for larger pieces of text. Now all I want is to simply load the text from name.html, but haven't yet found a way to do this. What should I try?
return mytemplate.render(name=open(<path-to-file>).read())
Thanks for the replies.
The idea is to use the mako framework since it does things like cache and check if the file has been updated...
this code seems to eventually work:
#route(':filename')
def static_file(filename):
mylookup = TemplateLookup(directories=['.'])
mytemplate = mylookup.get_template('index.html')
temp = mylookup.get_template('name.html').render()
return mytemplate.render(name=temp)
Thanks again.
Did I understand you correctly that all you want is read the content from a file? If you want to read the complete content use something like this (Python >= 2.5):
from __future__ import with_statement
with open(my_file_name, 'r') as fp:
content = fp.read()
Note: The from __future__ line has to be the first line in your .py file (or right after the content encoding specification that can be placed in the first line)
Or the old approach:
fp = open(my_file_name, 'r')
try:
content = fp.read()
finally:
fp.close()
If your file contains non-ascii characters, you should also take a look at the codecs page :-)
Then, based on your example, the last section could look like this:
from __future__ import with_statement
#route(':filename')
def static_file(filename):
mylookup = TemplateLookup(directories=['html'])
mytemplate = mylookup.get_template('hello/index.html')
content = ''
with open('name.html', 'r') as fp:
content = fp.read()
return mytemplate.render(name=content)
You can find more details about the file object in the official documentation :-)
There is also a shortcut version:
content = open('name.html').read()
But I personally prefer the long version with the explicit closing :-)

Categories

Resources