Static folder not reachable (Serving Angular apps on Flask) - python

I have an Angular app which I'm trying to serve from Flask server. The app runs on Flask but when I click a link on the app, the URL becomes ...../static/# instead of ...../# which results in PageNotFound error.
Basically, /static is being added to path. Do the files have to be placed in a specific way to avoid this issue? I have tried changing templateURL but don't quite know how this works.
This is what I've done: created template, static inside application. Copy-Pasted my Angular UI inside app. Created .py file to run the app. Built the angular app using ng build --base-href /static/. Then copied the .js files to static and index.html to templates folder.
The first page is alright but on clicking a link i go to ..../static/# which results in PageNotFound error.
IN A NUTSHELL, STATIC FOLDER CONTENT IS UNREACHABLE!
Also glyphicon isn't rendering properly when running on Flask but renders fine when running otherwise on localhost:4200. Instead it shows some other symbol on Flask. Some other styling also goes out of the window while using Flask but works otherwise.
Any suggestions are welcome.

Got it to work.
Just changed <a href="#"> to <a> and it started working!

Try building it using ng build --base-href or just ng build.
If this doesn't work then please show me your angular route and index.ts files.

In my projects, i never server static files from Angula/React/Vue from flask using CORS, i normaly create a separete HTTP server for frontend, it is egual how the server will be with Docker and Kubbernets,but if you prefer in this way, you can do something like this
from flask import send_from_directory,
#app.route('/<path:path>')
def send_static(path):
return send_from_directory(app.static_folder, path)

Related

How do I host my html flask app for Beanstalk?

I have a website in html with it's scrip.js and script.css, I need to get it hosted in BeanStalk. I am trying to develop it into a python/flask app for the Python platform on beanstalk. If I am correct it is creating a app.py .ebextentions and requirments.txt?
I am just struggling to understand it all, how it all works. Using Pycharm to create the scripts and from what I could see I need a src file with the website files in there or another script?
Templates files were shown in the example, that being where the website housed I think but struggling as a whole to host this an application in a Python/Flask format for Beasntalk. It is only a single page at this moment.
from flask import Flask
app = Flask(__name__)
#app.route('/<index.html>')
def page(index.html):
return render_template('index.html')
if __name__ == '__template__':
app.run(__template__)
This is the code I have found have been playing around with it but still errors.
If anyone has any insight to this anything will help.
Thank you all.
This video is basically what I have been trying to replicate. And I am very new to Python so sorry for that haha.
<"https://www.youtube.com/watch?v=dhHOzye-Rms&list=PLwbyvxAyI6CvcFr6mV-Z-HAhM1jTItE8V&index=7">

Jinja2 not work on flask/glitch.me deployment

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

Django doesn't show the CSS of my template

The problem is described in title. I have this template for a blog that I'm creating using Django.
When I open it normally, using double click over the HTML file, it looks like this:
But when I open it via url from the Django project, it looks like this
It only shows a green square (part of the css) but obviously can't open the css correctly.
Any idea to solve this?
In Django you don't open the HTML with double click on the file, you need to run the server first and open your site using the localhost (like you did in the second picture).
Judging by those images, are you sure you put the image in the static folder? In Django, the HTML files stays in the "templates" folder of your app and the css, javascript and images in the "static" folder.
If this answer doesn't help you, then you should post your code here, otherwise I can't find the problem.

Jinja 2 markups showing when i try to use apache with a flask python coded webpage

The website is accessible by other computers by inputting the ip address of the host computer.
However, the jinja2 markups are shown.
Jinja2 markups image
And the links which use url_for do not work either.
Non-working url image
What is the issue here?
Please help! Thanks!
flask / jinja is not a "server page" type of technology like PHP, so jinja templates (which by themselves know nothing about flask - jinja can be used from any Python code) won't be automagically "interpreted". You have to configure your apache server to serve content from flask as documented here : http://flask.pocoo.org/docs/deploying/mod_wsgi/

Django serving static pages from Sphinx

I have a large group of static html pages that I've generated from sphinx, and I'd like to show them on my Django site. I can connect to at least one page by putting the html/ folder form sphinx in a templates directory inside my app, and changing the urls.py file to include
url(r'^$', TemplateView.as_view(template_name="index.html")
Then it finds the index.html file inside myapp/templates/html. However, none of the internal links work (it'll try to redirect through Django and give me a 404 error). Also, the static files won't load in (Sphinx generates a _static folder, and even though I put that in the myapp directory, and the Chrome network tab tells me it's trying to load the css from myapp/_static, still nothing).
Is there any way to make all the links relative to each other inside this project? Alternatively, can I get Django to just serve up the whole project as static pages?
It looks like FlatPages is almost what I'm looking for, but I have more than just a title and content in these pages.
Firstly, you should not be using Django to serve essentially static files. This is what your webserver is for.
If you are using Apache, you can use the <directory> directive to serve these files alongside your Django routes.

Categories