Skip to content

update get_asset_url function - #1528

Merged
alexcjohnson merged 6 commits into
plotly:devfrom
karosc:dev
Jan 17, 2021
Merged

update get_asset_url function#1528
alexcjohnson merged 6 commits into
plotly:devfrom
karosc:dev

Conversation

@karosc

@karosc karosc commented Jan 15, 2021

Copy link
Copy Markdown
Contributor

This pull request addresses #1527 by adding and if statement to the get_asset_url function in dash.py instructing the app to use the external asset path if it is defined.

I have tested this with an short I wrote and published. If you run the app and examine the src attribute of the image, it references and image on my own site karosc.github.io.

- use external asset path if defined
@alexcjohnson

Copy link
Copy Markdown
Collaborator

Very nice! Could be a little DRYer, as it's only the first arg to get_asset_path that needs to change, but that's not a big deal.

Can you add a couple of unit tests of app.get_asset_url? Should cover both the local and external cases, make an app and test a couple of asset urls for that app, but we don't need to run the app (so the external url need not be valid). test_configs.py looks like a good place to put it, and it would look something like these two:

@pytest.mark.parametrize(
"name, server, expected",
[
(None, True, "__main__"),
("test", True, "test"),
("test", False, "test"),
(None, Flask("test"), "test"),
("test", Flask("other"), "test"),
],
)
def test_app_name_server(empty_environ, name, server, expected):
app = Dash(name=name, server=server)
assert app.config.name == expected
@pytest.mark.parametrize(
"prefix, partial_path, expected",
[
("/", "", "/"),
("/my-dash-app/", "", "/my-dash-app/"),
("/", "/", "/"),
("/my-dash-app/", "/", "/my-dash-app/"),
("/", "/page-1", "/page-1"),
("/my-dash-app/", "/page-1", "/my-dash-app/page-1"),
("/", "/page-1/", "/page-1/"),
("/my-dash-app/", "/page-1/", "/my-dash-app/page-1/"),
("/", "/page-1/sub-page-1", "/page-1/sub-page-1"),
("/my-dash-app/", "/page-1/sub-page-1", "/my-dash-app/page-1/sub-page-1"),
],
)
def test_pathname_prefix_relative_url(prefix, partial_path, expected):
path = get_relative_path(prefix, partial_path)
assert path == expected

After that, we'll just need a changelog entry and it'll be ready to go!

@karosc

karosc commented Jan 15, 2021

Copy link
Copy Markdown
Contributor Author

does this seem more DRY? Not sure exactly what you were referring to.

def get_asset_url(self, path):
        if self.config.assets_external_path:
            prefix = self.config.assets_external_path
        else:
	    prefix = self.config.requests_pathname_prefix

        asset = get_asset_path(
                     prefix,
                     path,
                     self.config.assets_url_path.lstrip("/"),
               )

        return asset

@alexcjohnson

Copy link
Copy Markdown
Collaborator

Yep, that's about what I had in mind. But again, not a big deal.

 - changed assets_external_path doc string to be more descriptive
 - revised _add_assets_resource to join assets_external_path with
 assets_url_path so that it is cogruent with how get_asset_path works
 - added unit test `test_asset_url`
 - updated changelog
Comment thread tests/unit/test_configs.py Outdated
Comment thread dash/dash.py Outdated
self.config.assets_external_path, url_path
res["external_url"] = "/".join([
self.config.assets_external_path.rstrip('/'), self.config.assets_url_path.strip('/'), url_path.lstrip('/')
]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe we could just call get_asset_url here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works! Passing all tests.

@alexcjohnson

Copy link
Copy Markdown
Collaborator

Looking good, just a couple of minor comments above, but also I think it needs linting (I sympathize with wanting aligned values in the test matrix but I don't think black will go for it 😅 ) - Probably just needs npm run format.

Co-authored-by: Alex Johnson <johnson.alex.c@gmail.com>
@karosc

karosc commented Jan 16, 2021

Copy link
Copy Markdown
Contributor Author

running npm format changed a bunch of other files, sorry for creating more review work...🤷‍♂️

@alexcjohnson

Copy link
Copy Markdown
Collaborator

Ah, I'm guessing you're on a newer version of black. What version do you have installed? We've talked about the need to update to 20.8b1 (and maybe now is the right time to do that across Dash, cc @Marc-Andre-Rivet?) but currently we have 19.10b0

black==19.10b0;python_version>="3.0"

@karosc

karosc commented Jan 16, 2021

Copy link
Copy Markdown
Contributor Author

oh shoot, you're right I am running 20.8b1. I can downgrade and reformat then if that's when you want to do.

@alexcjohnson

Copy link
Copy Markdown
Collaborator

Downgrading and reformatting would be the easiest way to get this merged quickly 😄 We may upgrade but that's going to take a little work because we'll want to do it evenly across all the dash repos.

@karosc

karosc commented Jan 16, 2021

Copy link
Copy Markdown
Contributor Author

Alright, I reset to before I ran the format routine, downgraded black to 19.10b0 and then redid the format. Things came out to be more expected since only my changes were reformatted by black.

@alexcjohnson alexcjohnson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice - thank you! 💃

@alexcjohnson
alexcjohnson merged commit 4ce87ee into plotly:dev Jan 17, 2021
alexcjohnson added a commit that referenced this pull request Jan 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants