Jinja2 not work on flask/glitch.me deployment - python

I have a deployment of a tiny Flask app on Glitch. It seems to be working... sort of. However, the Jinja2 template engine is clearly not working, since all Jinja2 tags are being read into the browser as text. The application, which I have up on my GitHub page, works fine on the localhost. Can I fix this somehow?

Seems like this was not a problem with earlier versions of Flask and Python.
Firstly, adding the following debug option in the server.py file.
app.debug = True
Secondly, add some code as follows:
#app.after_request
def apply_kr_hello(response):
"""Adds some headers to all responses."""
...
response.headers['Cache-Control'] = 'no-cache'
return response
Thirdly, do a Ctrl-Shift-R on the specific browser tab where your application is being rendered.
That is it! Now, when you make changes to static file, they should be reflected in your app.
I presume one might also benefit from reading https://stackabuse.com/serving-static-files-with-flask as well.

I'm not a python expert but while your glitch project has the same code as localhost, your local machine is executing the code through a different path. In this case because there are no instructions for Glitch to start your project from mesostic.py it's just treating your project as a static website. (hence why the template tags are just being rendered out as text).
It might help to look at other python projects on glitch to get a sense of how to execute your .py file. e.g. checkout start.sh on https://glitch.com/edit/#!/python3-morepath. I'm not sure but you may also need a requirements.txt to tell Glitch that this is a python project.
Hope this gives you a place to start debugging from

Related

Django admin site downloading style-sheets but not displayed as styled

I have set up a basic Django system and am testing out the admin app. The admin screen displays with raw styles and not the pretty ones shown in the tutorials and specified in the style-sheets.
Following several other questions, I found this is a common problem caused because Django doesn't normally serve static files. However, it does if DEBUG is switched on and I do have debug switched on. I followed all of the suggestions in the answers anyway to collect static files, etc.
If I enter the static file URLs directly they get downloaded and I can see the files in the developer mode of the browser (both Chrome and Edge). So I think the static files are being served.
Furthermore, if I save the page using the browser, it saves all of the static files and then if I open the main page it is shown with the correct styles. So the styles are working.
So it would seem to me that the files are being served and the browser is getting them and it can render them (from a saved file) but it somehow isn't working when served from Django.
Can anyone tell me what is going on?
EDIT:
Here is a strange thing: if, using the Chrome developer tool, I select the base.css file, click somewhere in the text of the CSS (say at the end of a line) and then add a space, suddenly the page appears correctly.
If I then do refresh the page it goes back to unstyled again.
EDIT 2:
I saw a recommendation to install the Whitenoise app to serve static file so I went ahead and did it. I turned off debug and presto! the styles appear. Turning on debug (so I presume django serves the files) and the styles go away. I saved both sites to the file system and compared the directories using a compare tool. There was no difference.
I'm not calling this an answer as the question is:
Why?
Also, I can't have debug on and get styles.

DevTools failed to load sourcemap problem: Could not load content

I have downloaded the free theme that can run to my Django server. I debug all things using static but the only front background image is not appearing on the server due to these problems in the console. Help me to get rid of this please.
First, open up your console.
Then, uncheck the circled checkboxes
and then reload.
I faced this problem while developing in ReactJS but I hope this will translate into bootstrap as well. However, I don't think the failed sourcemap thing actually brings side-effects to your code.
EDIT
If you have Grammarly enabled, do this:
go to chrome://extensions
And then change the extension to be manually enabled
After that, every time to want to use Grammarly, you either have to manually enable it (image below) or add a list of sites where you want it to run automatically (not on the site which you're running the server on).

How do you use a web.py application in Wordpress?

I have written an application in python to collect data from a javascript form and returned the processed text. It is based entirely off of the code here (but a lot more complex, so I have to use python for this).
https://kooneiform.wordpress.com/2010/02/28/python-and-ajax-for-beginners-with-webpy-and-jquery/
(note to people who like to edit...please leave this link in place since it shows all the relevant code sections in python and javascript).
I need to use this in wordpress (since that's what runs my site) and I honestly have no idea how to pull this off. Webpy can run with Apache CGI, but the documentation (http://webpy.org/cookbook/cgi-apache) is only clear if one wants to navigate directly to the python app as its own page.
I'm hoping someone here has expertise in how to embed this all within a Wordpress page/post?
Thanks!!
As far as I know, there is no native way to run Python code inside a WordPress site just like php. In fact, if you are not doing anything unique to Python, I would suggest you to use php, which supports regular expression and can be used in WordPress by installing the plugin "Insert PHP".
If you really want to use Python, then you need an API endpoint where you connect the function to your website. You would have to look into Azure Function App/AWS lambda on which you write a function app to work as a backend. Then whenever someone request your website, your website would do an HTTP request to that API.
Can you explain what exactly you want to do on your website?

Python - Want to change header logo based on url selection

I would like to change the logo of a website based on which menu is currently activated/seen by the user browsing the website.
For instance I have www.urltowebsite.com/menu1 = Header Logo 1
And then I have www.urltowebsite.com/menu2 = Header Logo 2
And on top of this I want to add an else statement stating that: If any other menu is selected, use header logo 3.
How can I make this possible with Python? I cant seem to wrap my head around what to define where and how to call up the different functions on the HTML website.
Oh and I insist doing this with Python. And preferably without any framework such as Django. But if needs be I can install web.py
EDIT:
Am I forced to go with php then? I would like to once and for all start utilizing Python on my web projects.
The website is made in simple HTML as I said first. The Javascript functions are only used to serve the HTML menu's through AJAX. Again this does not matter much for what I am trying to do, as menu's have classes and I can define those in php and thus change my logo/header.
What I want to do is to use Python in this instance. Here is a code snippet from the site:
<div id="header">
<span class="title"><img src="http://www.url.com/subfolder/images/logo.png"/>
</span>
</div>
And some more relevant to this:
<div id="menu">
<ul>
<li>001</li>
<li>002</li>
<li>003</li>
<li>004</li>
<li>005</li>
<li>006</li>
<li>007</li>
<li>008</li>
</ul>
</div>
So can I use python here?
You're asking to do the wrong thing the wrong way.
In order to change the logo based on the URL in Python , you need Python to generate the page and know what that url is.
There are two ways to do that in Python:
Use an existing Web Framework
Write your own Web Framework
"Python" doesn't know or care what your URL is - the frameworks and support libraries ( Django, Pyramid, Bottle, Flash, Tornado, Twisted, etc) figure out what the URL is by an integration with an underlying web server ( though some have their own webserver coupled in ). Similarly, PHP doesn't really know or care what the URL is - that information comes from an integration with Apache or FCGI/Nginx/etc. PHP tends to ship with most/all of that integration done. It's also worth noting that PHP is not just a language, but a web framework. Python is just a language.
Most Python frameworks will be written to the WSGI spec and have a "request" object that has all the data you want ( and many use the WebOb librbary for that ).
If you plan on doing everything with static HTML files, then you have a few options:
have a single static directory. use javascript to figure out the addressbar location, and render the corresponding logo / write the headers & footers.
have a "template" directory of all your HTML. use a Python script build a static version of each website with the custom headers/footers and configure your webserver to serve a different one for each domain.
No, Python cannot run inside the HTML web page. If you're really serving plain HTML pages then you must use javascript to execute code in the browser once the page is loaded. However, since you mention using AJAX, it sounds like it's not really true that you're serving plain HTML but rather have some server side code. If so, that server side code is the place to put your HTML-construction logic. To know the best way to do that, you would have to describe what's happening on the server.
Although I haven't used it, I have heard that the pyhp project more or less provides php-like embedded functionality for python.

Program web applications in python without a framework?

I am just starting Python and I was wondering how I would go about programming web applications without the need of a framework. I am an experienced PHP developer but I have an urge to try out Python and I usually like to write from scratch without the restriction of a framework like flask or django to name a few.
WSGI is the Python standard for web server interfaces. If you want to create your own framework or operate without a framework, you should look into that. Specifically I have found Ian Bicking's DIY Framework article helpful.
As an aside, I tend to think frameworks are useful and personally use Django, like the way Pylons works, and have used Bottle in the past for prototyping—you may want to look at Bottle if you want a stay-out-of-your-way microframework.
One of the lightest-weight frameworks is mod_wsgi. Anything less is going to be a huge amount of work parsing HTTP requests to find headers and URI's and methods and parsing the GET or POST query/data association, handling file uploads, cookies, etc.
As it is, mod_wsgi will only handle the basics of request parsing and framing up results.
Sessions, cookies, using a template generator for your response pages will be a surprising amount of work.
Once you've started down that road, you may find that a little framework support goes a long way.
You will have to look into something like CGI or FastCGI, which provides an API to communicate to the webserver.
Google App Engine enables you to write simple apps, and even provides a local webserver where you can try things out.
People here love frameworks. One shortcoming I have noted is that Python lacks a handy-dandy module for Sessions as a library built-in, despite it being available in PHP and as CGI::Session in Perl.
You will end up doing:
import cgi # if you want to work with forms and such
import cgitb; cgitb.enable() # to barf up errors to the web
print 'Content-type: text/html\n\n' # to start off any HTML.
You will have to write session stuff on your own.
For a PHP programmer, I think mod_python is a good way to get started without any framework. It can be used directly as Apache 2 module. You can have code tags (like <? ?> in PHP) and even conditional HTML output (HTML inside if statement):
<%
if x == y:
# begin
%>
... some html ...
<%
# end
%>
(simplified example taken from onlamp.com's Python Server Pages tutorial)
You should try web.py, it provides a bare minimum of features that does not get in your way.
http://webpy.org/
You can just make the entire thing yourself as a CGI script written in python. On an Apache server you go into the httpd.conf file and add these two lines at the bottom.
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
Then the standard output is redirected to the client, i.e. the print(...) method sends text to the client. Then you just read the .html, .css, and .js files stored on the server and print() each line. Connect to your database on the backend. Set up your security/authorization protocols... Basically you will need to make the entire framework yourself, only it will be customized to fit your needs perfectly.
Probably a good idea to come up with some special character to parse for when reading the files on the server and before printing to insert any dynamic content, such as:
HTML
<div>
<p> <<& pythonData $>> </p>
</div>
Python
htmlFile = open("something.html", "r")
for line in htmlFile:
if "<<&" in line:
# figure out what characters that special symbol is in the line
# replace them with dictionary value or variable or something
print(line)
else:
print(line)
Here is the documentation for the official library to work with Common Gateway Interface (CGI) in python: https://docs.python.org/3/library/cgi.html It includes an example that shows reading form data sent to the server into a python script.
Don't forget to tell your scripts where the python interpreter is on the Apache server (should be in /bin somewhere), in other words point at python with the sh-bang:
#!/bin/python3.10
Or wherever your server's python interpreter is located at.

Categories