python not reading in a web page - python

I run the python (server?) in the background that creates the pages etc.
I then have my usual index.html file and the header and nav bar are in a /shared folder.
On my index.html page, I have the following code :-
<html>
%include('shared/html-head.html')
%include('shared/html-nav.html')
for some reason, this is not loading the files. They are there and correctly named and are .html files.
The page in Chrome displays the following at the top of the page:- %include('shared/html-head.html') %include('shared/html-nav.html')
Any ideas why?

Related

How to generate .py file for enterprise MIBs using mibdump.py

I am trying to generate .py file for below MIB using mibdump.py:
mibdump.py --mib-source . --generate-mib-texts --destination-format pysnmp COMBA-REPEATERS-
RH7W22-v1.0.0.txt
It generates .py file with html header.
cat COMBA-REPEATERS-RH7W22.py
<html>
<head>
<script>
var forwardingUrl = "/page/bouncy.php?
........................
</html>
Can anyone suggest the correct command here? Or help me identify what I am missing here?
It's hardcoded to pull unknown mibs from snmplabs.com, and unfortunately that site is down. That result is an html redirect from whoever owns the domain snmplabs.com.
There's some resources here on where to find mibs: https://github.com/etingof/pysnmp/issues/376

Firefox or Chrome can't find a photo inserted in a Bottle template

I am old timer but young learner. I want make my own website, using Python and Bottle.
I have an HTML page which contains a title, a subtitle and a photo. This is a template used by Bottle in this little code:
from bottle import route, run, view
#route("/")
#view("pagedegarde.html")
def menu() :
contenu = "Moto de l'abbé Khan"
return {"titre" : "Bécanes", "contenu" : contenu}
run(host = '0.0.0.0', port = 8080, debug = True, reloader = True)
Here is the template, "gardepage.html":
<!doctype html>
<!-- page_de_garde.tpl -->
<HTML lang="fr">
<HEAD>
<TITLE>{{titre}}</TITLE>
<meta charset="UTF-8">
</HEAD>
<body>
<header>
<h1>Phrases et attrapes</h1>
</header>
<h3>{{titre}}</h3>
<p><image src="turquoise.jpg" alt="125 k4" /></p>
{{!contenu}}
<hr/>
</body>
</html>
The photo is in the same directory as the template and the python file.
Results :
127.0.0.1 - - [13/Mar/2022 11:10:58] "GET /turquoise.jpg HTTP/1.1" 404 746
The page is displayed, the title, the subtitle, but not the photo, there is instead the alternative mention "125 k4".
I wonder what it is, "746". Since I've had so many 404s (!), I've found that terminal messages always follow "404" with another number. I tried to find out about it, but couldn't find anything.
Otherwise, if I click on the html file, it displays without a problem, including the photo.
I tried both suffixes .tpl or .html, there is no difference. I tried .png or .jpg : no difference.
And I get the same result with Python 3.8.1 and Bottle 0.12.7 or with Python 3.10.2 and Bottle 0.12.19.
Thanks for reading me.
Bottle doesn't serve image/js/css automatically - you have to add own function for this.
It is simpler if you put static files in subfolder because then function can recognize this folder in url and run correct function. And this is popular method in other frameworks.
See: Routing Static Files
Example uses subfolder static for this - so you should have files
main.py
pagedegarde.html
static/turquoise.jpg
from bottle import route, run, view, static_file
#route("/")
#view("pagedegarde.html")
def menu() :
contenu = "Moto de l'abbé Khan"
return {"titre" : "Bécanes", "contenu" : contenu}
#route('/static/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='static/')
# or with full path
#return static_file(filepath, root='/full/path/to/static/')
run(host='0.0.0.0', port=8080, debug=True, reloader=True)
And remember to use /static in HTML
<p><image src="/static/turquoise.jpg" alt="125 k4" /></p>
Other frameworks may have the same problem. Some may serve static file only in debug mode but in normal mode they should be executed with web servers like Apache or nginx which should serve static files (because they do this faster)
EDIT:
To serve image in the same folder you would need to use regex to recognize filename in URL
#route('<filepath:re:.*\.(jpg|png|gif)>')
def server_static(filepath):
return static_file(filepath, root='')
The same you would have to do for other static files .css, .js, etc.
And if you would like to server other files for downloading or displaying then you would have to add also other extensions - ie. .csv, .xls, .mov, .mp3, etc.
If you would use regex .* as last route then it would serve all files (which don't match to previous routes)
#route('<filepath:re:.*>')
def server_static(filepath):
return static_file(filepath, root='')
but it is not safe because someone could run ie. http://0.0.0.0:8080/main.py to download source code.

Images appear fine when I preview HTML page directly, but appear broken when same page is returned from flask [duplicate]

This question already has answers here:
How to serve static files in Flask
(24 answers)
Link to Flask static files with url_for
(2 answers)
Closed 2 years ago.
working on a small project, for which I have an HTML page. For now it is a static page, with everything hard-coded.
When I preview it locally, it appears fine. But when the same page is returned from flask using render_template, the image link appears broken.
Following is the structure of directory:
/
-server.py
--templates/
---org_dashboard.html
---img_avatar.png
Im attaching screenshots as well as code snippets from return function, and the corresponding HTML code.
Python/flask code:
#app.route('/org_dashboard', methods=['GET', 'POST'])
def org_dashboard():
return render_template('org_dashboard.html')
Corresponding HTML code with image path:
<div class="card-columns">
<div class="card">
<img src="img_avatar.png" alt="Avatar" style="width:100%">
<div class="container">
<h4><b>John Doe</b></h4>
<p>Architect & Engineer</p>
</div>
</div>
</div>
When returned from localhost by flask. Notice the link appears broken:
Click here to view image
When viewed directly by opening the HTML file. Image appears fine Click here to view screenshot
The problem is that your image path is implying something that is only true in a static local HTML file.
src="img_avatar.png" tells the browser that the file is located in the same folder as the current page.
You need to change this to a relative path like this: src="/static/img_avatar.png" and then move the file to the /static folder in your project root.
Flask makes the assumption that you will do this and automatically adds a static view that takes a path relative to the project_root/static directory and serves it.

Creating a basic web page to trigger a Python script

I have a Python script that loops through a list of SQL queries read from an Excel file, executes them, and stores output results to a .csv file.
Currently I trigger this from the command line locally. I would like to create a very basic Web page, something that basically has a button to click to execute the Python script for now. If I can get the output file stored there somehow as well, even better.
The idea being I would start with it locally, but then move the web page somewhere where my team could access it and do the same thing.
I don't need much functionality at all for this web page obviously but this stuff is new to me so not quite sure where to start. Any ideas?
Thanks
I guess Flask would be a decent choice for a simple web app.
folder structure:
├── app.py
├── static
│   ├── index.html
app.py (EDIT added index.html route handling, duh doy)
from flask import Flask
app = Flask(__name__, static_url_path='', template_folder='static')
#app.route('/')
def index():
return app.send_static('index.html')
#app.route('/execute')
def execute_sql_logic():
# Do stuff here
return 'SQL executed', 200
if __name__ == '__main__':
app.run()
You'll need to export FLASK_APP=app.py in your working directory
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href='/execute'><button>execute SQL</button></a>
</body>
</html>
Simplest example I could think of.
Now when you'll run app.py and navigate to localhost:5000 (Port defaults to 5000 if I remember correctly) you'll see the HTML page with the execute SQL button. Clicking on it will send a GET to localhost:5000/execute which in turn will call your function.
Deploying on a server is beyond the scope of this answer unfortunately.

Template render on HTML AND CSS using Google App Engine

I'm currently working on my website hosted on GAE.
It has not been update since a while, so now, I'm trying to made a refresh of it :D
To do the trick, I try to use the MVC model using Python and WSGI, WebAPP2 and Render.Template.
Everything goes right, except for the CSS part.
Indeed, I can't render some part of my CSS using the GAE (django) method.
My Python controller is calling the HTML file and replace the variables by the dict() values correctly.
But now, and to be able to only have restricted amount of CSS file, I'm trying to do the same thing.
Unfortunatly I don't know how I'm suppose to call the CSS File.
I'm currently calling my CSS on my HTML as usual:
<link rel="stylesheet" media="screen" type="text/css" href="/assets/css/struct/index.css">
And trying to dynamically render this part of the file:
header#navigation{
height:auto;
min-height:480px;
width:100%;
min-width:100%;
background-image:url('/assets/img/content/{{content_cat_name}}/cat_img.jpg');
background-repeat:no-repeat;
background-position: left top;
background-size:contain;
background-color:#efefef;
}
and everything is then call by my python code like this:
class URIHandler(webapp2.RequestHandler):
def get(self, subdomain, page):
name = subdomain
pattern = os.path.join(os.path.dirname(__file__), '../views' ,'index.html')
template_values = {
'content_cat_name':name,
'cat_menu_title':name,
'cat_menu_text':name,
}
self.response.out.write(template.render(pattern, template_values))
So, if someone could help me to correctly call my CSS and replace the variables using my python script, I'll be really happy :D
Thanks in advance.
template.render can only replace tokens in the file that you specify in the path parameter (the first parameter). You're serving the .css file out of a static directory, so no token replacement happens, because that file's not getting passed through that code.
You could inline the parts of your CSS that contain tokens in your index.html file.
I was having what I think is the same problem. I found this GAE documentation very helpful. In short you need to go into your app.yaml file create a new handler:
url: /foldername
static_dir: foldername
And then in your link tag:
href="foldername/index.css"

Categories

Resources