The problem is that my index.html won't load a css file from a sister folder. I've tried a variety of Browsersync options and none worked.
Into my second day trying to figure this out. I'm working in the Flask framework for Python which works something like Laravel, etc. My task manager is Gulp, front end framework is Foundation 6. I'm new to Flask and Gulp. I used to use Grunt and Livereload with Laravel some years ago and had scss and browser reload working then. Memory fades though.
My file structure is supposed to be typical Flask, just the relevant folders here:
-root
-app
-static
-css
-js
-templates (html files)
-foundation (scss files and framework)
Browsersync seems to be designed so you have to have css under the html files. I've tested this with an index.html in the /root and /app folders and it works. However, I need / want the html in /templates.
In /app/templates/index.html:
<link rel="stylesheet" href="../static/css/app.css">
I'm using both command line for Browsersync and Gulp.js files in the root and in /foundation.
The Browsersync server will serve html from /templates if I set it up with "app/templates" but then it can't find the css. If I move /static/css into /templates the proper index.html file is rendered nicely in the browser. So Browsersync works with the old pre-app framework layout. It just can't deal with paths to sister folders.
gulp.task('serve', ['scss'], function() {
browserSync({
server: "app"
});
gulp.watch(src.css, ['scss']);
gulp.watch(src.html).on('change', reload);
});
I've considered their proxy option but so far can't find a solution with that. I haven't found many setup examples online and none were useful.
For now I'm just doing desktop layout of the app's html pages with Foundation 6 and haven't set up a dev server, just a folder on my MBP.
Any ideas? Please :-)
You can provide multiple base directories from which to serve static files
server: {
baseDir: ["app/templates", "static"]
}
this will server app/templates/index.html by default and then in your html just use
<link rel="stylesheet" href="/css/app.css">
This is my final working gulpfile.js in the site root and setup to work with Flask or most other application frameworks plus Foundation 6. Hope this example saves someone a day or more of figuring this out. I'll add js files later.
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserSync = require('browser-sync');
var sass = require('gulp-sass');
var reload = browserSync.reload;
var src = {
scss: 'foundation/scss/*.scss',
css: 'app/static/css/app.css',
allscss: 'foundation/scss/**/*.scss',
cssdest: 'app/static/css',
html: 'app/templates/*.html'
};
var sassPaths = [
'foundation/bower_components/foundation-sites/scss'
//'foundation/bower_components/motion-ui/src'
];
gulp.task('serve', ['sass'], function() {
browserSync({
open: false,
server: {
baseDir: ["app/templates", "app/static"]
}
});
gulp.watch([src.scss, src.allscss], ['sass'])
gulp.watch(src.html).on('change', reload);
gulp.watch(src.css).on('change', reload);
});
gulp.task('sass', function() {
return gulp.src(src.scss)
.pipe($.sass({
includePaths: sassPaths
})
.on('error', $.sass.logError))
.pipe($.autoprefixer({
browsers: ['last 2 versions', 'ie >= 9']
}))
.pipe(gulp.dest(src.cssdest))
});
gulp.task('default', ['serve']);
Related
I'm setting up a Heroku site to deploy a Django app for a school project. The problem is with static files using whitenoise in Django.
Quick context: My app consists of a form that takes 4 values that I use for quick math calculation inside a script. The aim of this script is to perform the calculations, draw a plot using matplotlib and save it in the static folder of my django app replacing the old one if it already exists. This static file is used to display in a html page on the site. Locally It works like a charm updating the plot each time I submit a new form. But when I try on Heroku it throws an
[Errno 2] No such file or directory: '/Users/jeff/Desktop/trydjango/src/static/yield_curve.png'
when I submit the form.
Here's the settings.py I have concerning static files:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
My directory look like this :
src
|-- /TER
|-- -- /settings.py
|-- /graph
|-- /static/...
|-- /staticfiles/...
|-- /manage.py
I would like for my site to refresh the image each time I submit a form using the new yield_curve.png I saved in the static folder.
If I had to guess, I would say that it has to do with the fact that static files have to be "static" and not change with time.
and save it in the static folder of my django app replacing the old one if it already exists
Yes, this is possible with Django. No, it's not possible on Heroku due to its ephemeral filesystem. You can overwrite your files, but that change will be lost the next time your dyno restarts. This happens frequently (at least once per day).
Heroku officially recommends storing user uploads and static files on a third party service like Amazon S3. Whitenoise disagrees. You can use Whitenoise on Heroku, but you can't (persistently) modify your static files without redeploying.
Note that this is true regardless of the plan your dynos use. Free or enterprise, dyno filesystems are ephemeral.
I believe you cannot save/update files with the free/basic Heroku account. Static files have to remain static and unchanged. I tried to do the same thing years ago with a school project myself. Here is how I pass values to my JS charts to render a figure dynamically so I don't have to use a file:
views.py
def some_function(request):
# Do your calculations on the data here
data = [1,2,3,4] # Let's say this is the results
# Pass data in your context to the template
context = { "my_data" : data }
my_template.html
<!-- Before including your JS file -->
<script> var my_passed_data = {{ my_data|safe }} </script>
<script "include your JS file (test.js in this example) which uses the my_passed_data to make a figure>
test.js
// Whatever library you use to render a chart will be here, I'm using CanvasJS for this example
var chart = new CanvasJS.Chart("chartContainer", {
** all your options, etc. **
data: [
{
** all the other stuff such as type, name, etc. **
dataPoints: [
{ x: 0, y: my_passed_data[0] }, // here is where you access your passed data by index
{ x: 2, y: my_passed_data[1] },
This is how I pass data from my View to Template to JS to render charts using CanvasJS and without needing any files. I'm not sure if this is the best way but it works for me, hope this helps and good luck with your school project!
How to use fontawesome in ionic 2 rc0 as there is no gulp/grunt file so I can add file into build process?
Here's an article i came across while searching on this same topic.
https://chriztalk.com/ionic-2-font-awesome-using-sass/
Here's a gist:
Make a new config directory the root of your ionic 2 project:
$ mkdir config
Find the copy.config.js and sass.config.js files into this folder: /node_modules/#ionic/app-scripts/config/ and copy them into the folder you just created.
Add these lines to the new copy.config.js in config directory you just created.
...
copyFontAwesomeCSS: {
src: '{{ROOT}}/node_modules/font-awesome/css/font-awesome.min.css',
dest: '{{WWW}}/assets/css/'
},
copyFontAwesome: {
src: '{{ROOT}}/node_modules/font-awesome/fonts/**/*',
dest: '{{WWW}}/fonts/'
},
...
Add these lines to the includePaths[] block of the sass.config.js file in the config directory you just created:
...
includePaths: [
...
'node_modules/font-awesome/scss',
...
],
...
Add a config block in your package.json file with the following references:
...
"config": {
"ionic_copy": "./config/copy.config.js",
"ionic_sass": "./config/sass.config.js"
},
....
Import font-awesome in your app.scss file:
...
#import 'font-awesome';
...
Finally, use font awesome like you would in any html file:
<h1><i class="fa fa-flag" aria-hidden="true"></i> Font Awesome</h1>
There is still a lot of confusion on what is a best practice when it comes to adding FontAwesome to an ionic2 app, so I wrote an article about it to mitigate some of that confusion. I hope this helps anybody else looking for a correct answer
http://luiscabrera.site/tech/2017/01/09/fontawesome-in-ionic2.html
Simply add cdn css link of font awesome to your index.html
Or you can use #import of sass to add it to your project
#import 'lib/fa.css'
I'm using Django 1.9, React and webpack, I used externals to load the django I18n functions such as gettext in my javascript files.
This is loaded like this from a view :
<script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' %}/0"></script>
Here is my webpack config :
externals: {
// require("jquery") is external and available
// on the global var jQuery
"jquery": "jQuery",
"utils": "utils",
"gettext":"gettext",
"django":"django",//I18n functions are encapsulated in the django object
},
In my jsx files I load gettext like this :
import {gettext, interpolate, ngettext} from 'django'
render(){
var login_header_text = gettext("blablabla.");
....
Actually almost everything works, when I use django-admin makemessages -d djangojs -l it recovers many gettext, however some gettext are ignored by the script so sometime I have to put the gettext at the beginning of my render function and then it works..., I don't know why this is happening.
In the end with my technic I can make it work totally but maybe I did something wrong. Maybe I should generate mo file with grunt and load it dynamically with po loader with webpack because I will need soon to load them dyamically into the page.
Maybe you can guide me a bit ? Thanks
redmine uses the favicon placed at /usr/share/redmine/public/favicon.ico
I found a lot of code snippets using cd /usr/share/redmine/; grep -HR favicon app/
app/helpers/application_helper.rb: def favicon
app/helpers/application_helper.rb: "<link rel='shortcut icon' href='#{favicon_path}' />".html_safe
app/helpers/application_helper.rb: # Returns the path to the favicon
app/helpers/application_helper.rb: def favicon_path
app/helpers/application_helper.rb: icon = (current_theme && current_theme.favicon?) ? current_theme.favicon_path : '/favicon.ico'
app/helpers/application_helper.rb: # Returns the full URL to the favicon
app/helpers/application_helper.rb: def favicon_url
app/helpers/application_helper.rb: path = favicon_path
app/views/journals/index.builder: xml.icon favicon_url
app/views/common/feed.atom.builder: xml.icon favicon_url
app/views/layouts/base.html.erb:<%= favicon %>
But no luck finding more info about how to set the favicon_path or favicon_url.
Workaround:
I added a small javascript in the theme folder: javascripts/theme.js:
document.head = document.head || document.getElementsByTagName('head')[0];
function changeFavicon(src) {
var link = document.createElement('link'),
oldLink = document.getElementById('dynamic-favicon');
link.id = 'dynamic-favicon';
link.rel = 'shortcut icon';
link.href = src;
if (oldLink) {
document.head.removeChild(oldLink);
}
document.head.appendChild(link);
}
changeFavicon('../themes/freifunk-red-andy/images/favicon.ico');
(But that workaround only works if the visitor uses javascript)
Redmine automatically loads the first file it finds inside the favicon sub-directory of your theme. Thus, if you put your favicon into e.g. favicon/favicon.ico, it will be automatically used by Redmine.
I think this question is ambiguous as there are 2 places you have to consider depending what you really want to do.
If you want to change the favicon of ...
... an additionally installed theme you have to go with #Holger Just 's answer (https://stackoverflow.com/a/27440983/887930) and copy your own favicon into the folder redmine/htdocs/public/themes/YOURTHEME/favicon/ (overwrite an existing favicon.ico or create the folder favicon inside the theme's folder if necessary).
... the standard preinstalled redmine theme you have to copy your own favicon into the folder redmine/htdocs/public (overwriting the existing favicon.ico)
I've successfully precompiled handlebars into one file using grunt-ember-templates, however, when including source in html file:
<script src="templates/app/compiledTemplates.js" type="text/javascript"></script>
It says this:
Resource interpreted as Script but transferred with MIME type text/plain: "file:///Users/jaime/voyant-svn/voyant-blocks/dev/blocks-web/src/main/webapp/templates/app/compiledTemplates.devjs".
What is the proper way of including precompiled handlebar templates?
I combine the compiled templates with my other scripts (jQuery/ember/ember-data/my app code) using grunt. Then in my index.html I simply include the single js script (helps cut down on the number of http requests also).
I'm currently using grunt for this, a simple "build" step might look something like the below. To use this you would need to install nodejs. Next you would npm install the following
grunt
grunt-cli
grunt-ember-template-compiler
grunt-contrib-concat
Once you have these installed you can run the build below from your root if you put the js into a file called "Gruntfile.js" -then simply execute "grunt" and it will output a deps.min.js (w/ your script and templates combined)
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-ember-template-compiler');
grunt.initConfig({
concat: {
dist: {
src: [
'website/static/website/script/vendor/handlebars-v1.2.1.js',
'website/static/website/script/vendor/ember.min.js',
'website/static/website/script/my-app.js’,
'website/static/website/script/dist/tmpl.min.js'],
dest: 'website/static/website/script/dist/deps.min.js'
}
},
emberhandlebars: {
compile: {
options: {
templateName: function(sourceFile) {
var newSource = sourceFile.replace('website/static/website/script/app/templates/', '');
return newSource.replace('.handlebars', '');
}
},
files: ['website/static/website/script/app/templates/**/*.handlebars'],
dest: 'website/static/website/script/dist/tmpl.min.js'
}
}
});
grunt.task.registerTask('default', ['emberhandlebars', 'concat:dist']);
};
This is how I do it in my app:
<script type="text/javascript" charset="utf-8" src="dist/templates.js"></script>
You can see the whole index.html file here:
https://github.com/joachimhs/WarmCroc-Ember/blob/master/index.html
In, fact, I just wrote this code today during a live-coding presentation on Ember.js. The talk is recorded as a screencast, and is available from http://emberjstraining.com
This talks should give you the pointers you need to get everything set up properly :)