Parsing / using routes or query string

I’m planning out the app I’m about to build, and one of the features we require is sending out secret links to people to click on and join the session.

I tried to find it in the api documentaion but I didn’t have any luck. How would I go about inspecting the url on the first load and open a particular form?

Hey,

What you’re looking for is the get_url_hash() function (docs here). You can see it in action in the custom user authentication example – it’s what I use for the “confirm your email address” link.

2 Likes

You can make Google aware of the location hash by using a fragment that begins with a !, for example foo.anvil.app/#!store.

1 Like

Hey,
What you’re looking for is the get_url_hash() function (docs here ). You can see it in action in the custom user authentication example – it’s what I use for the “confirm your email address” link.

For anyone like me who can never remember how to access the hash (and inevitably winds up on this page every few months,) the documentation has moved to here: Anvil Docs | Navigation

Hi Ben,
I am looking for a way to pass a query string to my app, for example:

https://myapp.anvil.app/#?a=foo&b=bar 

Using the docs (Anvil Docs | Navigation),
I am using the following, in my start-up Form’s def init code:

self.label_1.text = f"Our URL hash is: {get_url_hash()}"

But I am getting nothing returned from get_url_hash()
Am I going about this the wrong way?
Chris

You might want to have a look at Hash Routing by @stucork

2 Likes

It’s working for me: https://fake-cylindrical-row.anvil.app/#?a=foo&b=bar

https://anvil.works/build#clone:BCGVN3CZE6TVSXTJ=U7H7SHJGTNBWNGSEQ5U7YFTB

hugetim,
Thanks. I cloned your app and it said

“You’ve opened an app that uses Python libraries which
aren’t available on free Anvil plan.”

I am using the free Anvil plan, however, I was able to copy your code.

But when I tested it using my app: https:// and

test-query-strings.anvil.app/#?a=foo&b=bar

nothing appeared in the label texts.

I guess its only for the paid plan?

Chris.

Owen,
Thanks, I will definitely have a look at Hash Routing, although it looks like its much more than I need.
Do you know if it will work for the free Anvil plan?
Regards, Chris.

Yes, it’s entirely client side, so fine on the free plan.

The paid plan shouldn’t affect this. (To confirm, I’ve switched something in the clone app above that should prevent that message from coming up at all.)

This is odd because when I visit https://test-query-strings.anvil.app/#?a=foo&b=bar, I see this:

Are you seeing something different? If so, what browser are you using? Any privacy/ad-blocker browser extensions that could be interfering?

hugetim - strangely, it is now working for me also? I don’t know which browser I was using when it did not work, but I have re-tried on all my browsers, Firefox, Edge, Vivaldi, Brave and Opera
and they all now display Our URL hash is: {‘a’: ‘foo’, ‘b’: ‘bar’}

I have not rebooted, nor logged out of Anvil - so it is a complete mystery, why it was not working before.
Thanks, anyway for your help.
Chris

1 Like