Forum Discussion
REST API TO CREATE NOTEBOOK
- 1 year ago
Hello sambhubiswal - thanks for posting.
Based on your description, I believe you are able to create the notebook object in the workspace but the notebook definition is not included in it so it doesn't work. Please let me know if this is not correct. The info below applies to this scenario.
Are you getting any errors returned from the API call, like InvalidItemType, ItemDisplayNameAlreadyInUse, CorruptedPayload? These can help us figure out what might be the problem.
Here are a few things to check:
- Verify that the definition is correctly formatted - it should include the format (such as ipynb) and the parts with the correct path, payload, and payloadType (such as InlineBase64).
- Verify that the payloads for the .py and .platform files are correctly encoded in base64.
Here is a sample script:
POST https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/notebooks { "displayName": "Notebook 1", "description": "A notebook description", "definition": { "format": "ipynb", "parts": [ { "path": "notebook-content.py", "payload": "Base64EncodedContent", "payloadType": "InlineBase64" }, { "path": ".platform", "payload": "Base64EncodedContent", "payloadType": "InlineBase64" } ] } } - 1 year ago
Hi jennratten ,
Thank you for your response. I am trying to read a ".py" & a ".platform" file from my local drive, converting those contents to base64 encoding and using those as payload to create the same notebook inside a workspace using REST API call. For my testing purpose, I am using below code to create a sample notebook. Although the notebook created successfully, but it's throwing error "Language name undefined is not valid for synapse_pyspark kernel." when I open the notebook inside workspace. It will be very helpfull if you have any thing which I can reffer to achieve this.
Code I use to create notebook is:using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace NotebookCreation { class Program { static async Task Main(string[] args) { string baseUrl = $"https://api.fabric.microsoft.com/v1/workspaces/{WorkspaceID}/notebooks"; var authService = new AuthService(); string apiKey = await authService.GetAccessTokenForFabric(); var notebookName = "Notebook_Test"; var notebookDescription = "A notebook description"; var notebookContent = @" { ""cells"": [], ""metadata"": {}, ""nbformat"": 4, ""nbformat_minor"": 4 }"; var notebookPayload = Convert.ToBase64String(Encoding.UTF8.GetBytes(notebookContent)); var requestPayload = new { displayName = notebookName, description = notebookDescription, definition = new { format = "ipynb", parts = new[] { new { path = "notebook-content.ipynb", payloadType = "InlineBase64", payload = notebookPayload } } }, kernelSpec = new { name = "synapse_pyspark", language = "python" } }; var jsonPayload = Newtonsoft.Json.JsonConvert.SerializeObject(requestPayload); using var client = new HttpClient(); client.BaseAddress = new Uri(baseUrl); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); try { var response = await client.PostAsync(baseUrl, content); if (response.IsSuccessStatusCode) { Console.WriteLine("Notebook created successfully."); } else { var responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Error: {response.StatusCode}, {responseBody}"); } } catch (Exception ex) { Console.WriteLine($"Exception occurred: {ex.Message}"); } } } }
Hi sambhubiswal,
I wanted to follow up since we haven't heard back from you regarding our last response. We hope your issue has been resolved.
If the community member's answer your query, please mark it as "Accept as Solution" and select "Yes" if it was helpful.
If you need any further assistance, feel free to reach out.
Thank you,
Pavan.