Template error , while creating a simple website using python - 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>

Related

Linebreak of a yaml not displaying at an html file

I am having a ymal file and a html file where some of the content in the ymal file is loaded into the html file (see below). I would like to add linebreaks to the string in the ymal file such that it is shown in separate lines when the html file is loaded. I am using flask and jupyter to load the html file.
The html file is loaded correctly but whatever i try it seems that I cannot get the string to break where I want it to.
Does anyone can help me to add linebreaks in the yaml files such that they are shown in the html file?
Thank you.
python code:
import oyaml as yaml
from flask import Flask
from flask import render_template
app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False
#app.route('/')
def index():
website_data = yaml.load(open('_config.yaml'))
return render_template('home.html', data=website_data)
if __name__ == '__main__':
app.run()
My yaml code:
profile:
first_name: |
I would like to have a break here.\n
And then I would also to have a break here\\
But it does not work whatever I try</br>.
My html code:
<!DOCTYPE html>
<html>
<body>
<h1>My homepage</h1>
<p>{{ data.profile.first_name }} </p>
<p>This is a test website again</p>
</body>
</html>

How to call a python function which contains input function to a HTML

I have built a function in python that takes user input using input() and stores that into SQL.
I want to build a front end with flask and when I'm trying to call this python function in Html it displays none.
I tried a similar solution provided over StackOverflow, but as my function contains input(), it is not working.
def my_link():
x=input('your name: ')
return (x)
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Click me
</body>
</html>
I want my Html page to initiate the flask function i.e ask for user input and further my function also stores that user input to SQL server.
But when I'm trying to call this function(which has user input) over HTML, it displays none.

Flask redirect stops working after one try?

When I run flask with python test.py, and then navigate to http://localhost:5000 in my browser, I would expect the terminal to read:
test-1 test-2 test-3
The three print statements ONLY appear when test.py is run for the first time. After that, the terminal will only show:
test-1 test-2
For some reason, the redirecting to the second page is not occurring after the initial run. Is there some sort of weird caching nonsense going on? Can someone please explain what is happening?
from flask import Flask, redirect, url_for, send_from_directory
app = Flask(__name__)
#app.route('/')
def home():
print('test-2')
return redirect(url_for('page2'))
#app.route('/secondPage')
def page2():
print('test-3')
return send_from_directory('.', 'test_dashboard_4.html')
if __name__ == '__main__':
print('test-1')
app.run(debug=True)
I was finding lying around and I found this question is old and no answers yet.
So I will be the one who may help you.
Use render_template and put all of your htmls to templates folder. Something like this
import flask
app = flask.Flask("My life sucks")
#app.route('/')
def index():
return flask.render_templates('your html page here.')
if __name__ == '__main__':
app.run(debug=True)
One other thing, you can't put javascript to same directory where html was.
Instead put it in static folder and add static to where you import javascript like.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<body>
<h1>
Hello world!
</h1>
<script src="static/hi.js"></script>
</body>
</html>
This is javascript file if you want to see something
console.log("I get imported!");
document.body.innerHTML += '<p>:D</p>'
Here's the tree
D:.
│ app.py
│
├───static
│ hi.js
│
└───templates
index.html

Python Flask to change text in a HTML file

I'm trying to create a simple program which will replace {{ test }} with 'Hello world' by following a tutorial, however I am stumped and when I open the HTML file - as {{ test }} is shown on the page instead of 'Hello World' which is what should be appearing.
Any help would be appreciated because I am very unsure on what to do to fix this, thanks.
I am unsure if I have even linked the two files, as to my knowledge it was never specified in the video and I have only just noticed that there is no link between the two files.
Python Code:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def homepage():
return render_template('index.html', test='hello world')
if __name__ == '__main__':
homepage()
else:
print('Please run this file as main, not as module.')
HTML Code:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<p> {{ test }} </p>
</body>
</html>
Flask is a webserver. You are not meant to call the functions with app.route. Replace the last part with:
if __name__ == '__main__':
app.run()
and then visit http://127.0.0.1:5000/ in your browser. The template file is not meant to change.
If for some reason you don't want to run a server but you just want to create HTML files, then use Jinja2, the template engine behind Flask.

Difficulty accessing json file with d3 and flask

I am using Flask as a web framework, and I am trying to implement the first example from the book Getting Started with D3, by Mike Dewar. I have a Python script named run.py and two directories, templates/ and static/, containing index.html and service_status.json, respectively. Unfortunately, my code is not rendering the data at all, nor is it producing any glaring errors.
This is what I have in run.py:
#!/usr/bin/env python
from flask import Flask, render_template, url_for
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
if __name__=="__main__":
port = 5000
app.debug = True
app.run( port=port )
This is what I have in templates/index.html:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<META CHARSET="utf-8">
<SCRIPT SRC="http://d3js.org/d3.v3.min.js"></SCRIPT>
<SCRIPT>
function draw(data) {
"use strict";
d3.select("body")
.append("ul")
.selectAll("li")
.data(data)
.enter()
.append("li")
.text( function(d){
return d.name + ": " + d.status;
}
);
}
</SCRIPT>
<TITLE>MTA Data</TITLE>
</HEAD>
<BODY>
<H1>MTA Availability Data</H1>
<SCRIPT>
d3.json("{{ url_for( 'static', filename='service_status.json') }}",draw); // <---- BIG PROBLEM
</SCRIPT>
</BODY>
</HTML>
I am using Windows 7, Google Chrome, and Python 2.7.
If the JSON file is not going to change, then you should put it in the static directory and use
from flask import url_for
url_for('static', filename='service_status.json')
For this to work, also change the path in the JavaScript to '/static/service_status.json'
Static files like your json document, are by default served from a different directory from the templates - by default 'static'
You dont need to use the url_for call in your view, you can use it in your template:
d3.json("{{ url_for('static', filename='service_status.json') }}",draw);
So to summarise: 1) Move your json document in to the static folder (a folder called static along side your templates folder, by default), and 2) use the url_for call in your template to get the correct URI for your json document.
If you want to use a folder other than static, you can change that by passing static_folder to the Flask object contructor
You seem to be getting a 304 status code as you mentioned in earlier comments. I see that your JSON has the following date/time:
"Date": [
"12/15/2011"
],
"Time": [
" 7:35AM"
],
I am not 100% sure but this might help:
http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#if-modified-since
Basically, it says that
"This request header is used with GET method to make it conditional: if the requested document has not changed since the time specified in this field the document will not be sent, but instead a Not Modified 304 reply.
Format of this field is the same as for Date:"
So, may be you can check the timestamp on the JSON and probably just do a fresh save ?

Categories