Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now
Hi fabricators, is there a function by which we can see the name of the workspace or the workspace ID in fabric notebook?
Solved! Go to Solution.
Hi @igorkk
Thanks for using Fabric Community.
You can use the below codes:
Hope this helps. Please let me know if you have any further questions.
Although marked as solved, it really isn't, so here's a solution...
How to get the workspace name when running a notebook. Use sempy (semantic link)
Here’s the function page help
https://learn.microsoft.com/en-us/fabric/data-science/semantic-link-semantic-functions
Install like this
%pip install -U semantic-link
Then run this...
import sempy.fabric as fabric
# Get the id of this notebooks workspace
workspace_id = fabric.get_notebook_workspace_id()
# You can also get this with fabric.get_workspace_id()
# From all workspaces, filter by Id and then just get the Name
workspaces = fabric.list_workspaces()
workspace_row = workspaces[workspaces.Id == workspace_id]
workspace_name = workspace_row['Name']
Edit: In the above code I put a comment to say that you could also use fabric.get_workspace_id(). Due to a possible bug in Fabric, you cannot use this, and should only use fabric.get_notebook_workspace_id(), like I did in the above code.
Thanks to Element115 for mentioning this. See replies for details.
There's a simpler, one line, way to get the workspace name. Behold!!! 😁
import sempy.fabric as fabric
workspace_name = fabric.resolve_workspace_name()
Nice find. Sempy is evolving nicely and so too is notebookutils (mssparkutils) with another one-liner.
notebookutils.mssparkutils.env.getWorkspaceName()
or
notebookutils.runtime.context.get("currentWorkspaceName")
However, something I just noticed and don't know if it's a bug or intended to be such. Here's the context.
2 workspaces: SPARK and IVDS. 1 notebook: nb_test. Current workspace == SPARK.
I created nb_test in workspace IVDS. Later I saved it as a copy to workspace SPARK.
I am now working in SPARK-->nb_test.
This
workspace_id = fabric.get_notebook_workspace_id()
returns 'SPARK'.
But this
workspace_id = fabric.get_workspace_id()
returns 'IVDS' or the workspace where the copy's original was first created.
Go figure.
I agree that this looks like a bug.
As your code references id's, the return values will be workspace GUIDs, not names, but I assumed you meant the ids are referencing different workspaces and was intrigued so I tried it out, and I found exactly the same thing. fabric.get_workspace_id() gets the pre-copy workspace id. and if you used it to subsequently get the workspace name from a lookup using it, the result would be incorrect. get_notebook_workspace_id() returns the correct value so probably a better choice.
Rather more worrying, I also note that if you deploy with deployment pipelines rather than using Save a copy, you still get this behaviour.
(tested on 2024-09-24)
To get the workspace name, using notebookutils.mssparkutils is probably easier, but this library fdoesn't have a method to get the workspace_id
mssparkutils.env.getWorkspaceName()
It's not my code but your code btw 😁, I lifted from your older post.
import sempy.fabric as fabric
def get_current_workspace_name():
# Get the id of this notebook's workspace
workspace_id = fabric.get_notebook_workspace_id()
# From all workspaces, filter by Id and then just get the Name
workspaces = fabric.list_workspaces()
workspace_row = workspaces[workspaces.Id == workspace_id]
workspace_name = workspace_row['Name'].values[0] # Extract the first value as a string
return workspace_name
# Usage example
workspace_name = get_current_workspace_name()
print(f'name: {workspace_name}')
Hi @igorkk
Thanks for using Fabric Community.
You can use the below codes:
Hope this helps. Please let me know if you have any further questions.
Due to a change to notebookutils, these snippets work now:
fabric_workspace_id = notebookutils.runtime.context.get("currentWorkspaceId") # current workspace ID
fabric_workspace_name = notebookutils.runtime.context.get("currentWorkspaceName") # current workspace name
Could you please share the link where this is documented?
Hi,
I tried this and it works as expected for the Id, but when I call:
Hi @igorkk
We haven’t heard from you on the last response and was just checking back to see if your query got resolved. Otherwise, will respond back with the more details and we will try to help.
Thanks
Hi @igorkk
We haven’t heard from you on the last response and was just checking back to see if your query got resolved. Otherwise, will respond back with the more details and we will try to help.
Thanks
Hi @igorkk
Glad that your query got resolved. Please continue using Fabric Community for any help regarding your queries.
Check out the October 2024 Fabric update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
4 | |
2 | |
1 | |
1 | |
1 |