I work in Django, and trying to load the css file of the Glyphicon for i can use that icons.
I download the css file named "bootstrap.css" from bootstrap, and then upload this file to my statics files on the "css" directory.
The staticfile loading its not the problem, becaues another css files i put in "css directory" are work.
Should i do more actions instead of just upload "bootstrap.css" to my static files? i saw there (in the downliaded zip file) a "map" file for that css file, but i didnt think it connected.
You need to download glyphicon fonts and put them in the ../fonts directory (path relative to your bootstrap.css) so your path structure resembles that:
your_static_dir
├── css
│ └── bootstrap.css
└── fonts
├── glyphicons-halflings-regular.eot
├── glyphicons-halflings-regular.svg
├── glyphicons-halflings-regular.ttf
└── glyphicons-halflings-regular.woff
look at path in "bootstrap.css" line 266
src: url('../fonts/glyphicons-halflings-regular.eot');
src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
static
css
bootstrap.css
bootstrap.css.map
...
fonts
glyphicons-....eot
glyphicons-....svg
glyphicons-....ttf
glyphicons-....woff
glyphicons-....woff2
js
...
Related
So I initialized CAS using cas-initializr with the following command inside the cas folder:
curl https://casinit.herokuapp.com/starter.tgz -d "dependencies=core,bootadmin,metrics,gitsvc,jsonsvc,redis,support-themes-core" | tar -xzvf -
My theme name is example. Here is the file structure of cas/src/main/resources/templates:
templates
├── META-INF
│ └── spring.factories
├── application.yml
├── example.properties
├── static
│ └── themes
│ └── example
│ ├── css
│ │ └── cas.css
│ ├── images
│ │ ├── favicon.ico
│ │ └── logo.png
│ └── js
│ └── cas.js
└── templates
└── example
└── casLoginView.html
The content of casLoginView.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
The content of example.properties:
cas.theme.name=Example Theme
cas.theme.description=Example - Central Authentication Service
cas.standard.css.file=/themes/example/css/cas.css
cas.standard.js.file=/themes/example/js/cas.js
cas.logo.file=/themes/example/images/logo.png
cas.favicon.file=/themes/example/images/favicon.ico
cas.drawer-menu.enabled=false
cas.notifications-menu.enabled=false
cas.login-form.enabled=true
My cas.properties file located at /etc/cas/config/:
cas.server.name=https://localhost:8443
cas.server.prefix=${cas.server.name}/cas
cas.serviceRegistry.json.location=file:/etc/cas/services
# cas.authn.accept.enabled=false
server.ssl.key-store=file:/etc/cas/thekeystore
server.ssl.key-store-password=changeit
cas.theme.defaultThemeName=example
spring.thymeleaf.cache=false
After I started the server with ./gradlew bootRun, I opened https://localhost:8443/cas/login on my chrome browser. The CSS, Image and JS seems to load fine (I confirmed it using Chrome Devtool), but the default template was used instead of my custom template.
I had tried googling around for several hours now and I still can't figure out how to make cas load my html template.
P/s: I tried moving all the resources content to cas/build/resources, cas/build/resources/main but it does not seem to work either.
Starting with 6.4 RC5 (which is the version you run as of this writing and should provide this in your original post):
The collection of thymeleaf user interface template pages are no longer found in the context root of the web application resources. Instead, they are organized and grouped into logical folders for each feature category. For example, the pages that deal with login or logout functionality can now be found inside login or logout directories. The page names themselves remain unchecked. You should always cross-check the template locations with the CAS WAR Overlay and use the tooling provided by the build to locate or fetch the templates from the CAS web application context.
https://apereo.github.io/cas/development/release_notes/RC5.html#thymeleaf-user-interface-pages
Please read the release notes and adjust your setup.
All templates are listed here:
https://apereo.github.io/cas/development/ux/User-Interface-Customization-Views.html#templates
What is the proper way of serving static files (images, PDFs, Docs etc) from a flask server?
I have used the send_from_directory method before and it works fine. Here is my implementation:
#app.route('/public/assignments/<path:filename>')
def file(filename):
return send_from_directory("./public/assignments/", filename, as_attachment=True)
However if I have multiple different folders, it can get a bit hectic and repetitive because you are essentially writing the same code but for different file locations - meaning if I wanted to display files for a user instead of an assignment, I'd have to change it to /public/users/<path:filename> instead of /public/assignments/<path:filename>.
The way I thought of solving this is essentially making a /file/<path:filepath> route, where the filepath is the entire path to the destination folder + the file name and extension, instead of just the file name and extension. Then I did some formatting and separated the parent directory from the file itself and used that data when calling the send_from_directory function:
#app.route('/file/<path:filepath>', methods=["GET"])
def general_static_files(filepath):
filepath = filepath.split("/")
_dir = ""
for i, p in enumerate(filepath):
if i < len(filepath) - 1:
_dir = _dir + (p + "/")
return send_from_directory(("./" + _dir), filepath[len(filepath) - 1], as_attachment=True)
if we simulate the following request to this route:
curl http://127.0.0.1:5000/file/public/files/jobs/images/job_43_image_1.jpg
the _dir variable will hold the ./public/files/jobs/images/ value, and then filepath[len(filepath) - 1] holds the job_43_image_1.jpg value.
If i hit this route, I get a 404 - Not Found response, but all the code in the route body is being executed.
I suspect that the send_from_directory function is the reason why I'm getting a 404 - Not Found. However, I do have the image job_43_image_1.jpg stored inside the /public/files/jobs/images/ directory.
I'm afraid I don't see a lot I can do here except hope that someone has encountered the same issue/problem and found a way to fix it.
Here is the folder tree:
├── [2050] app.py
├── [2050] public
│ ├── [2050] etc
│ └── [2050] files
│ ├── [2050] jobs
│ │ ├── [2050] files
│ │ └── [2050] images
│ │ ├── [2050] job_41_image_decline_1.jpg
│ │ ├── [2050] job_41_image_decline_2554.jpg
│ │ ├── [2050] ...
│ ├── [2050] shop
│ └── [2050] videos
└── [2050] server_crash.log
Edit 1: I have set up the static_url_path. I have no reason to believe that that could be the cause of my problem.
Edit 2: Added tree
Pass these arguments when you initialise the app:
app = Flask(__name__, static_folder='public',
static_url_path='frontend_public' )
This would make the file public/blah.txt available at http://example.com/frontend_public/blah.txt.
static_folder sets the folder on the filesystem
static_url_path sets the path used within the URL
If neither of the variables are set, it defaults to 'static' for both.
Hopefully this is what you're asking.
having below directory structure.
home/
└── jobs/
├── jobname1/
│ ├── builds/
│ └── config.xml
└── jobname2/
├── builds/
└── config.xml
having many job names under jobs folder.
I wish to copy all config.xml files to another backup directory without missing its jobname folder structure.
could you please help me understand how to use shutil libraray in more efficient way.
My target folder Backup structure something like below
Backup/
└── jobs/
├── jobname1/
│ └── config.xml
└── jobname2/
└── config.xml
Please help.
Please find below my actual requirement.
Algorithm to write python2.7 scrpt
1. git clone Backup folder , where Backup is empty folder
2. Find list of files having file name "config.xml in the HOME folder
3. Copy all config.xml files to Backup folder with out changing the folder structure
3.1. If config.xml is new file then git add all config.xml to git repository , and commit followed by git push
3.2. If config.xml is existing and unchanged then git push is not required
3.3. If config.xml is existing and modified then commit followed by git push
I found the logic. May be this might be useful to someone. Regret the code as it is novice level.
def walkfs(self,findfile):
## dictionary to store full path of each source findfile
matches_fullpath_dict = {}
## dictionary to store relative path of each findfile
matches_trunc_dict = {}
## dictionary to store full path of each target file location
matches_target_fullpath_dict = {}
i =0
source = self.jenkins_Home
destination = self.backupRepositoryPath
for root, dirnames, filenames in os.walk(source):
for filename in fnmatch.filter(filenames, findfile):
i=i+1
matches_fullpath_dict[i]=os.path.join(root, filename)
matches_trunc_dict[i]=os.path.join(root.replace(source,""), filename)
matches_target_fullpath_dict[i]=os.path.join( os.path.sep, destination + root.replace(source,""))
keys = matches_target_fullpath_dict.keys()
for key in keys:
if not os.path.exists(matches_target_fullpath_dict.get(key)):
try:
os.makedirs(matches_target_fullpath_dict.get(key))
except OSError:
pass
keys = matches_target_fullpath_dict.keys()
for key in keys:
shutil.copy2(matches_fullpath_dict.get(key,None), matches_target_fullpath_dict.get(key,None))
I have following directory structure:
A
|
|--B--Hello.py
|
|--C--Message.py
Now if the path of root directory A is not fixed, how can i import "Hello.py" from B to "Message.py" in C.
At first I suggest to add empty __init__.py file into every directory with python sources. It will prevent many issues with imports because this is how the packages work in Python:
In your case it this should look like this:
A
├── B
│ ├── Hello.py
│ └── __init__.py
├── C
│ ├── Message.py
│ └── __init__.py
└── __init__.py
Let's say the Hello.py contains the function foo:
def foo():
return 'bar'
and the Message.py tries to use it:
from ..B.Hello import foo
print(foo())
The first way to make it work is to let the Python interpreter to do his job and to handle package constructing:
~ $ python -m A.C.Message
Another option is to add your Hello.py file into list of known sources with the following code:
# Message.py file
import sys, os
sys.path.insert(0, os.path.abspath('..'))
from B.Hello import foo
print(foo())
In this case you can execute it with
~/A/C $ python Message.py
This question already has answers here:
How to serve static files in Flask
(24 answers)
Closed 2 years ago.
In my Flask app. If the page's path is http://example.com/product/6. I can't import js file in this page's html file like this :
<script src="js/main.js"></script>
The browser will search the js file in http://example.com.product/js/main.js, and return 404 error.
So, what should I do to fix this ?
You should make your static resources use the root directory (if that's where you're keeping them all).
So if your directory looks like this:
.
├── app.py
└── static
├── css
├── img
└── js
└── main.js
try adding a / onto your javascript URL so it isn't just appended onto the page URL.
e.g. <script src="js/main.js"></script> will then go to http://example.com/js/main.js.
FYI - You should be using a web server such as apache or nginx to server your static files as they are a lot better at it than Flask.
You should use url_for to generate the URLs for your static assets.
<script src="{% url_for('static', filename='js/main.js' %}"></script>