flask_jsondash icon indicating copy to clipboard operation
flask_jsondash copied to clipboard

ValueError: urls must start with a leading slash

Open Thuruv opened this issue 7 years ago • 25 comments

I know somewhere I made a mistake but downloading and running it plainly gave me this error. I can understand somewhere in the routing the error is but I can find it, Can you help?

Traceback (most recent call last): File "app.py", line 17, in <module> app.register_blueprint(charts) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 64, in wrapper_func return f(self, *args, **kwargs) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 951, in register_blueprint blueprint.register(self, options, first_registration) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\blueprints.py", line 151, in register endpoint='static') File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\blueprints.py", line 76, in add_url_rule view_func, defaults=defaults, **options) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 64, in wrapper_func return f(self, *args, **kwargs) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\flask\app.py", line 1043, in add_url_rule rule = self.url_rule_class(rule, methods=methods, **options) File "C:\Users\c_thv\AppData\Local\Continuum\Anaconda2\envs\coffeemug\lib\site-packages\werkzeug\routing.py", line 603, in __init__ raise ValueError('urls must start with a leading slash') ValueError: urls must start with a leading slash

Thuruv avatar Jun 21 '17 10:06 Thuruv

It looks like there is an invalid url somewhere in your flask app. Are you using the existing example app, or importing it into your own app instance? If the latter, make sure all your url routes have a leading slash, e.g.:

@app.route('foo/bar/')
def foobar():
    pass

would need to become:

@app.route('/foo/bar/')
def foobar():
    pass

christabor avatar Jun 21 '17 19:06 christabor

Just did a quick check and all the routes in the example app.py, example endpoints.py, and the core blueprint charts_builder.py have the correct leading slash.

christabor avatar Jun 21 '17 19:06 christabor

@Thuruv I'll close this one if you've been able to isolate the problem, otherwise let me know, thanks!

christabor avatar Jul 11 '17 17:07 christabor

Hi @christabor ,

I run into the same error. It might be an error that arise from the platform, as I am using MS Windows.

I get the same traceback as @Thuruv and find that when I put in a print() before the error it is related to static files. I had a look at chart_builder.py line 50. Here you use static_url_path=STATIC_DIR. This might not trough an exception on UNIX based systems, but on Windows, the path starts with a letter (fx C:...). The static_url_path should probably just be static_url_path="/STATIC/" or something. When I correct like that, the error is gone.

Kr

madslundgaard avatar Sep 29 '17 20:09 madslundgaard

@madslundgaard Thanks, I've re-opened it and will take a look!

christabor avatar Oct 02 '17 16:10 christabor

@madslundgaard does this work for you? I don't have a windows VM to test with at the moment. You can git apply this as a patch.

diff --git i/flask_jsondash/charts_builder.py w/flask_jsondash/charts_builder.py
index 46c04c4..cc5e4b9 100644
--- i/flask_jsondash/charts_builder.py
+++ w/flask_jsondash/charts_builder.py
@@ -34,9 +34,16 @@ from .schema import (
     validate_raw_json, InvalidSchemaError,
 )
 
+IS_WINDOWS = os.name == 'nt'
 TEMPLATE_DIR = os.path.dirname(templates.__file__)
 STATIC_DIR = os.path.dirname(static.__file__)
 
+# The __file__ attribute does not appear to correctly handle slashes
+# for Windows. See https://github.com/christabor/flask_jsondash/issues/128
+if IS_WINDOWS:
+    TEMPLATE_DIR = os.path.normpath(TEMPLATE_DIR)
+    STATIC_DIR = os.path.normpath(STATIC_DIR)
+
 # Internally required libs that are also shared in `settings.py` for charts.
 # These follow the same format as what is loaded in `get_active_assets`
 # so that shared libraries are loaded in the same manner for simplicty

christabor avatar Oct 02 '17 16:10 christabor

Hi @christabor, I don't think you need to change how the 'DIR' variables are assigned, as this is related to the URL for static files only.

madslundgaard avatar Oct 02 '17 18:10 madslundgaard

For your correction, the normpath will only change slashes, not the file drive which will still be C:... I think

madslundgaard avatar Oct 02 '17 18:10 madslundgaard

Ok, I think I understand a bit more clearly. However, the STATIC_DIR and TEMPLATE_DIR still need to be dynamically generated. This is so that the package can be imported and registered as a blueprint correctly when being installed with our without a virtualenv, instead of just cloning and copying code into a project.

But the issue of the drive letter and colon being prepended still remains. A different solution would be to regex match and then replace the first bit, e.g. C:\path\to\virtualenv\flask_jsondash\static\ becomes \path\to\virtualenv\flask_jsondash\static\. But does this even guarantee it will work? I am not sure if flask can resolve the file path with that.

Either way, it does feel a bit hackish, but there are no obvious workarounds that I can see.

christabor avatar Oct 02 '17 23:10 christabor

Hi, You don't need to change the two variables, as they don't make any runtime errors where they are used for DIR assignment, only when they are used in the URL: charts = Blueprint( 'jsondash', name, template_folder=TEMPLATE_DIR, static_url_path=STATIC_DIR, <== HERE static_folder=STATIC_DIR,

That assignment could just be left out, then it should default to /STATIC/, or if it need be another URL path /some/URL/path/ could be specified.

madslundgaard avatar Oct 03 '17 03:10 madslundgaard

Same problem here trying to run example app in windows 10

Susensio avatar Feb 18 '18 14:02 Susensio

In line 149 of blueprints.py, self.static_url_path equals C:\Users\... ...\flask_jsondash\static (with my username and full path). The problem is it starts with C. I've change that line to

state.add_url_rule('/static' + '/<path:fi
                   view_func=self.send_st
                   endpoint='static')

and now it works.

Susensio avatar Feb 18 '18 14:02 Susensio

I had the same problem, but it is working on windows 10 with VMWARE. I installed this project in Ubuntu

ghost avatar Mar 21 '18 16:03 ghost

this problem still exists. just updating

maverik-akagami avatar Apr 20 '18 21:04 maverik-akagami

same too

quanpower avatar Dec 01 '18 15:12 quanpower

+1 win10 python3.6...

+1 win10 python 2.7.8

randomgenerator2019 avatar Dec 20 '18 22:12 randomgenerator2019

Sorry folks, I'll try to put some time into this over the weekend.

christabor avatar Dec 21 '18 17:12 christabor

same problem

nmaas87 avatar Feb 28 '19 23:02 nmaas87

Hello, I have tried all the possible approaches but still having the same issue. ValueError: urls must start with a leading slash

I am not getting where exactly I need to fix this issue.

ShaileshSarda avatar Aug 28 '20 08:08 ShaileshSarda

Hi, i tried some approaches as well, but indeed, the same error on windows 10 +python 3.9 here. Someone got this ?

Chuckpy avatar Mar 19 '21 00:03 Chuckpy

Hello, I'm facing the same error too. Did anyone come across a solution?

rajatsaxena0707 avatar Mar 28 '21 13:03 rajatsaxena0707

Does anyone have a solution for this yet?

utsav-enact avatar Oct 25 '21 06:10 utsav-enact

The fix for us atleast was https://github.com/dgtlmoon/changedetection.io/commit/7e0f0d0fd8a2ad1d621e342e19155ab578539234#diff-fff535675c4953d2d9411ee893a00a3abcd2b966b698123616479e6cd3985015

basically remove the full path from static_url_path :)

dgtlmoon avatar Mar 23 '22 09:03 dgtlmoon

Thank You so much, it works

Usman4862 avatar Jan 29 '23 18:01 Usman4862

I had the same error with my odoo 11 project, windows 10 and python 3.7.

There is a URL that doesn't start with a leading slash (/) in the routing map :

routing_map.add(werkzeug.routing.Rule(url, endpoint=endpoint, methods=routing['methods'], **kw))

Simply replace the line your odoo code by this line :

routing_map.add(werkzeug.routing.Rule('/'+url.lstrip('/'), endpoint=endpoint, methods=routing['methods'], **kw))

Hopefully it works !

OumeymaJouini avatar Mar 25 '23 23:03 OumeymaJouini