Sinatra can't see styles.css - sinatra

In my directory I have:
shop.rb (App < Sinatra::Base)
lib directory - Ruby models inside
views directory - erb files inside
Gemfile
Where should I place my style.css ? I tried every place possible, I created public folder and tried with also every possible place, but my styles doesn't work.
In layout I have:
<link rel="stylesheet" href="/style.css"/>
I tried also with:
<link rel="stylesheet" href="style.css"/>

It might be because the app is not configured to find the public directory/ First make sure the public directory exists relative to the main entry file of your application (assuming shop.rb) and use the following configuration:
class Shop < Sinatra::Base
configure do
set :public_folder, File.expand_path('../public', __FILE__)
set :views , File.expand_path('../views', __FILE__)
set :root , File.dirname(__FILE__)
end
get '/' do
erb :index
end
end
Once set up, Sinatra should now know where to find your public directory and you can just include the asset file like this:
CSS:
<link rel="stylesheet" href="/style.css"/>
Image:
<img src="/img/background.png" alt="Image" />
JavaScript:
<script src="/js/main.js"></script>

Related

Local module import tree possibility?

In my local package, there is a tree of modules, like this:
# source/main.py
from source import worker
def run():
return worker.func()
# source/worker.py
def func():
return 'hello'
I want to demonstrate this package with PyScript (before my package needs to be installed), but do not want to rewrite the whole package for the sake of PyScript. This "index.htm" calls the main.run(). It is OK until the main wants to import the worker module so that it can call its func().
<head>
..
<py-env>
paths:
- source/main.py
- source/worker.py
</py-env>
</head>
<body>
<py-script>
import main
main.run()
</py-script>
</body>
The "import worker" would work, but as I stated above, it is "from source import worker" and cannot be changed just to make it work in PyScript.
Could I define somehow that the root for the local module imports is above "source", and imports like "import source.module.py" should work?
It is not possible to do
from source import worker
because source is not a module, it is a folder. Pyscript is not same as python. The whole context is different. There is no need for it also. You can have it all together working and reachable if you declare nodes py-env and py-script like below.
index.html in application root folder:
<html>
<head>
<link rel="stylesheet" pyscript.css" />
<script defer src="pyscript.js"></script>
<!--<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js" onerror=scriptLoadFailure('pyscr ipt.js')></script> -->
<py-env>
paths:
./source/worker.py
./source/main.py
</py-env>
</head>
<body>
<py-script src="./source/worker.py"></py-script>
<py-script src="./source/main.py"></py-script>
</body>
</html>
The modules in py-env are declared with exactly 4 spaces indentation (no tab char) and starting from application root folder ( ./ ) folowed by subfolder and module. In body section both modules are declared as src attribute of py-script nodes. This way they are like unioned and operational without import one or other nor from one import something. You can just call the function from wherever you need to.
The modules are both in the source subfolder (though I prefer puting main in the app root along with index.html). There is just one change in main.py where I put the call of run() function for it was excluded from html.
Modules in ./source/ subfolder are
#
# source/main.py
def run():
return func()
run()
#
#
# source/worker.py
def func():
return 'hello'
Open it in browser and you'll see hello (tested with Chrome & Edge) meaning that run() command from main.py has called func() function from worker.py without importing anything.
And if you realy need to keep those imports all I can suggest is to do a replace of those imports to end up like below. It can be done with any editor and it will work both with python and pyscript.
try:
from source import worker
except:
pass
Regards...
Since the current PyScript does not do such, I created a patched version and proposed the PyScript project to pull it into a future version. See the ticket with my comment and the attached zip containing the solution:
Add Local Package Loading to Py-Env #519

Unable to access js and style sheet using Play

Here I import from view file:
<script src='javascripts/tic-tac-toe/main.js'></script>
<link rel="stylesheet" type="text/css" href=stylesheets/tic-tac-toe/style.css'>
public folder structure:
[]
routes file:
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
When I try to access the view page I receive following error in firefox console and the files are not loaded :
The resource from
“http://localhost:9000/assets/stylesheets/tic-tac-toe/style.css'” was
blocked due to MIME type (“text/html”) mismatch
(X-Content-Type-Options: nosniff).
How to access js and css resources from view file ?
I can browse directly locally to the files on the browser via the address bar:
http://localhost:9000/assets/javascripts/tic-tac-toe/main.js
http://localhost:9000/assets/stylesheets/tic-tac-toe/style.css
Seems I need to prepend with "assets":
<script src='/assets/javascripts/tic-tac-toe/main.js'></script>
<link rel="stylesheet" type="text/css" href='/assets/stylesheets/tic-tac-toe/style.css'>

How to get Codeigniter 3 to use project root instead of referencing full file path on localhost for key folders

I've looked around and I'm sure this is common but can't seem to get the right answer.
I've just recently set up a Codeigniter 3 project on my localhost and a migrating some Codeigniter 2 code.
The index.php file is a bit different.
I can get the project up and running except the main folders references in the index.php file such as APPLICATION and others I've added like "ASSETS" etc are loading like this:
<link href="http://[::1]/projectOne/E:\public_html\projectOne\assets\css\style.php?1537317115" rel="stylesheet" type="text/css" >
Even when I set a base_url:
<link href="http://localhost/projectOne/E:\public_html\projectOne\assets\css\style.php?1537317115" rel="stylesheet" type="text/css" >
I've checked it wasn't just the folders I added by echoing out the APPPATH variable:
define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);
echo APPPATH;
Which produces:
E:\public_html\projectOne\application\
What am I missing here?
How do I get my paths to folder set in the index.php to just use the project root, and not the whole filepath on the computer?
Figured it out. For anyone else that hits this issue.
This new part in CI3:
if (($_temp = realpath($application_folder)) !== FALSE)
{
$application_folder = $_temp;
}
Allows you to more securely/easily have your application folder outside of the website root.
My additional ASSETS folder was then showing the full web path which you obviously don't want.
So commenting out this part leaving only:
$application_folder = strtr(
rtrim($application_folder, '/\\'),
'/\\',
DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
);
And then setting you base_url value in the config.php file gives your CSS, JS and IMAGES proper site relative paths.

How to change the config file for MathJax in doxygen?

I am using doxygen 1.8.9.1 and want to use AsciiMath in the hmtl output. It requires me to use a new config file, i.e. http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML. However, the configuration "MATHJAX_RELPATH" can only set the root path of mathjax.
How could I change the config to mathjax? Thanks for any suggestions.
EDIT:
I have tried to use MATHJAX_CODEFILE to change the config file "config: ["local/local.js","MMLtoHTML.js"]", but don't work for me. The URL should be modified, not just the configuration files. (i.e. MathJax.js?config=AM_HTMLorMML).
BTW: The render html should be change from \[sum_(i=1)^n i^3=((n*(n+1))/2)^2\] to `sum_(i=1)^n i^3=((n*(n+1))/2)^2`.
One solution is to modify MATHJAX section of footer.html in the Doxygen folder.
For example, use this to modify the url to MathJax.js?config=local/local.js
<!-- BEGIN MATHJAX-->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script><script type="text/javascript" src="../mathjax/MathJax.js?config=local/local.js"></script>
<!-- END MATHJAX-->
The $mathjax line of header.html should also be deleted.

How to deploy a Polymer dart application

I have a dart application using Polymer.dart, i replace the dart.js file by the boot.js (in my index.html), an now i want to deploy this application in order to have a working javascript one.
But, when i launch the build.dart, i have :
Total time spent on web/index.html -- 504 ms
Total time -- 555 ms
but nothing is created, no more "out" folder.
And when I do Pub Deploy, it create a "deploy" folder, but that version is not working (because the build.dart doesn't properly work i think).
Have you any idea?
Right now, it's a two step process. I suspect this will get easier. In the meantime:
Create a build.dart that looks like this:
import 'package:polymer/builder.dart';
main() {
build(entryPoints: ['web/index.html'], options: parseOptions(['--deploy']));
}
Take note, currently, a lot of warnings and hints are produced. You can ignore them for right now.
For the current version i have done these steps :
type on your console:
dart build.dart --deploy
dart2js out/web/index.html_bootstrap.dart -oout/web/index.html_bootstrap.dart.js
index.html:
<!DOCTYPE html>
<html>
<head>
...
<link rel="import" href="clickcounter.html">
<script type="application/dart">export 'package:polymer/init.dart';</script>
<script src="packages/browser/dart.js"></script>
</head>
<body>
...
<div id="sample_container_id">
<click-counter count="5"></click-counter>
</div>
</body>
</html>
build.dart:
import 'package:polymer/builder.dart';
main(args) {
build(entryPoints: ['web/aaa.html'],
options: parseOptions(args));
}