Insert image in python web.py - python

I have a file test1.py. I am using web.py to display it in a webpage.
import web
urls = ('/', 'hello','/bye', 'bye')
app = web.application(urls, globals(), True)
class hello:
def GET(self):
return """<html>
<head>
</head>
<body>
<img src="smile.png" alt="SAP Logo" width="500px" height="100px"/>
</body></html>"""
However, when run it will display:
127.0.0.1:59558 - - [28/Sep/2015 15:52:32] "HTTP/1.1 GET /" - 200 OK
127.0.0.1:59558 - - [28/Sep/2015 15:52:32] "HTTP/1.1 GET smile.png" - 404 Not Found
Image is placed in Desktop.

Create a "static" folder in your project root, copy your "smile.png" to that folder. Now change the src value of the img tag:
<img src="/static/smile.png" alt="SAP Logo" width="500px" height="100px"/>
You should see your smile.png in the brower now, for more info on serving static files in web.py, go visit "http://webpy.org/cookbook/staticfiles".

Related

Android emulator how to display image to remote destination in flask

I was referring to this github https://github.com/ahmedfgad/AndroidFlask and https://heartbeat.comet.ml/uploading-images-from-android-to-a-python-based-flask-server-691e4092a95e. I was unable to display the images into the remote destination from android emulator. I dont think the source code that they provide, do display images to the remote destination using flask.
Below is my github. It shows all the changes I have made.
https://github.com/2100723/AndroidEmulator/tree/main. I have made changes to the flask_server.py and images_template.html which is located in the templates folder . I need in the image to display in the remote host.
My android emulator has managed to save my images from emulator in the file call static whenever press connect to server & upload enter image description here. All I left is to send my image to a remote destination.
#Flask_server.py
import flask
import werkzeug
import time
app = flask.Flask(__name__, static_folder='static')
#app.route('/', methods = ['GET', 'POST'])
def handle_request():
image_paths = []
files_ids = list(flask.request.files)
print("\nNumber of Received Images : ", len(files_ids))
image_num = 1
for file_id in files_ids:
print("\nSaving Image ", str(image_num), "/", len(files_ids))
imagefile = flask.request.files[file_id]
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("Image Filename : " + imagefile.filename)
timestr = time.strftime("%Y%m%d-%H%M%S")
file_path = timestr+'_'+filename
imagefile.save(file_path)
image_paths.append(filename)
image_num = image_num + 1
print("\n")
return flask.render_template('image_template.html', images=image_paths)
app.run(host="0.0.0.0", port=5000, debug=True)
#image_template.html
<!doctype html>
<html>
<head>
<title>Image Gallery</title>
</head>
<body>
<h1>Image Gallery</h1>
{% for image in images %}
<img src="{{ url_for('static', filename=image) }}" alt="image" width="200">
{% endfor %}
</body>
</html>
I try changing my Flask_server.py code and adding image_template.html to make my code to display to the remote server using flask. Nothing is display in the remote destination using flask

Set Background image in CSS file using python flask framework render_template

I need to set a background image for my html. I have a file system of
Static
css
img
This is what I use to link my css file.
<link rel="stylesheet" href="{{ url_for('static',filename='css/index.css') }}">
In my css file I defined the background-image
.bgimg-1 {background-image: url("/static/img/background.jpg");}
However in my flask application I am getting the error of
127.0.0.1 - - [09/Aug/2020 19:10:04] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [09/Aug/2020 19:10:04] "[33mGET /static/css/static/img/background.jpg HTTP/1.1[0m" 404 -
Im not sure where does the /static/css/ is coming from
I defined the class in <header class="bgimg-1 w3-display-container w3-grayscale-min" id="home">
I feel, it's because your index.css file is inside /static/css/ folder and from there it is referring to image (/static/img/background.jpg) , so making the complete image url to this - /static/css/static/img/background.jpg

Template error , while creating a simple website using python

I was creating a simple website using web.py framework . But when i ran the website it gave template error.
My html file code
def with (greeting)
<html>
<head>
<title> "This is a web project"</title>
<body>
<H1>"Yoyo honey singh"</H1>
<H6>greeting<H6>
</body>
</head>
</html>
My python file code
import web
urls = ("/","Index")
app = web.application(urls,globals())
render = web.template.render("\templates")
class Index:
def GET(self):
greeting = "HELLO WORLD"
return render.index(greeting = greeting)
if __name__ == "__main__":
app.run()
I placed the index.html file in the template folder and the python script in the bin folder. The directory structure is like
D:\WEBSITE
WEBSITE
NAME
init.py
bin
app.py
tests
init.py
docs
templates
According to the documentation you should put a $ before each python statement in the template. Therefore the first file becames:
$def with (greeting)
<html>
<head>
<title> "This is a web project"</title>
<body>
<H1>"Yoyo honey singh"</H1>
<H6>$greeting<H6>
</body>
</head>
</html>

Web.py not serving static files

Running on the devel server, I can render 0.0.0.0:8080/index.html without CSS fine, but no static files load. 0.0.0.0:8080/static results in "Not found" and 0.0.0.0:8080/static/style.css gets a 404.
All I've googled quotes or links to http://webpy.org/cookbook/staticfiles, which I feel like I've followed to point.
filetree
code.py
/web
...
/static
style.css
/images
...
/pages
index.html
code.py
import web
render = web.template.render('/home/keith/code/webpy/skeleton/pages/')
urls = (
'/', 'index',)
class index:
def GET(self):
return render.index()
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
/pages/index.html
...
<link rel="stylesheet" type="text/css" href="static/style.css" />
...
Would appreciate any help!
I tried running python code.py from a terminal window and the static files served fine! Before, I was using the terminal emulator in my IDE (Geany 1.24.1). So that was the problem, for whatever reason.

Serving image with flask

I have a program which generates an image. Now I want to use Flask to make this picture accessible to other users, but I can’t display this image with the following code:
#!/usr/bin/python2
#coding: utf-8
from flask import *
app = Flask(__name__)
#app.run(host='0.0.0.0')
#app.route('/')
def index():
return render_template('hello.html')
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')
My template hello.html is:
<!doctype html>
<title>Hello from Flask</title>
<h1>Hello World!</h1>
<img src="./weather-plot.png">
When I run this program and visit the page, I see this:
192.168.0.61 - - [10/Jul/2013 10:22:09] "GET / HTTP/1.1" 200 -
192.168.0.61 - - [10/Jul/2013 10:22:09] "GET /weather-plot.png HTTP/1.1" 200 -
And in my browser I see the title, but not the picture. What’s wrong?
By the way, is there a better method to display a picture without anything else? Maybe I don’t have to use a template?
Are you sure the image is indeed in the location ./, i.e. in the root of your project?
In any case, it is better to use Flask's url_for() method to determine URLs (see http://flask.pocoo.org/docs/api/#flask.url_for) This makes sure that when you move things around, the URLs don't break.

Categories