PBI-Guy / blog

Find here all material used for my blog https://aka.ms/PBIGuyBlog
62 stars 21 forks source link

NameError: name 'df_result' is not defined #2

Open peya02 opened 8 months ago

peya02 commented 8 months ago

Hello, I have copied the code to do some testing and it gives me an error in the NameError part: name 'df_result' is not defined. Did you mean: 'result'?

for index, row in df_result.iterrows():
    current_value = df_result['Fiscal Year'][index]
    body = {
        "format":"PDF",
        "paginatedReportConfiguration": {
            "parameterValues":[
                    {"name": "DateFiscalYear", "value": current_value}
                ]
        }
    }

    api_call = post_request(export_url, header, body)
    export_id = api_call.json()['id']
    export_id_list.append(export_id)
    parameter_value_list.append(current_value)

Do you know what it could be?

CrasCris commented 6 months ago

I think so, cuz he never define it, or he turn the result dict into a dataframe, did you solve it ?

Yep he did in the blog image

peya02 commented 6 months ago

Hello, I didn't get it, but I see that I don't have the part you indicate. Is the full code available? I want to implement it in an application that I already have in python - flask thank you.

CrasCris commented 6 months ago

Hello, I didn't get it, but I see that I don't have the part you indicate. Is the full code available? I want to implement it in an application that I already have in python - flask thank you.

i didn't do it yet, as soon as i done it, i will post it, i'm doing with Flask as well

peya02 commented 6 months ago

Okay perfect, I will be attentive to your publication. Thank you.

CrasCris commented 6 months ago

Okay perfect, I will be attentive to your publication. Thank you.

I try it, but you need to have a Premium Licence, this repo can embedding the report https://github.com/microsoft/PowerBI-Developer-Samples, but the export of the model as the format you want you need the licence

peya02 commented 6 months ago

That repository is exactly the one I use, I modified it a little to my liking but basically it is the structure. But the issue that drives me a little crazy is exporting to PDF, yes I have managed to export but basically what it does is like a "screenshot", if for example you have a table with many records in the report, having the slider table, those final values will be left out of the capture.

CrasCris commented 6 months ago

I didn't know that, the "screenshot", is a liquid PDF ?

peya02 commented 6 months ago

I don't know what you mean by liquid PDF, but what I have tried through this website is that it takes a screenshot of what you see at that moment and converts it to PDF.

https://learn.microsoft.com/en-us/rest/api/power-bi/reports/export-to-file-in-group?tryIt=true&source=docs#code-try-0

peya02 commented 6 months ago

I am attaching an image of what could be a power bi table, as it has many rows and columns, not all of them appear, because the API takes a screenshot of what is being seen at that moment and passes it to you in PDF, leaving the rows/columns that are not being seen.

image

CrasCris commented 6 months ago

I see , thanks for help , i try it, but i dont have the licence so i get this image

CrasCris commented 6 months ago

If you try CSV format, Did you get all the data in the visuals ?

peya02 commented 6 months ago

I have not tried it although you can now get csv directly from the options of the power bi table itself.

CrasCris commented 6 months ago

I have not tried it although you can now get csv directly from the options of the power bi table itself.

I need the information of the report, so i dont mind if in csv format, can you show me the code to export the report ? i have tried with the but i only get a 403 error , i dont know , if i'm doing it right

peya02 commented 6 months ago

To export to csv there is no code, you can do it from the power bi desktop table itself

CrasCris commented 6 months ago

To export to csv there is no code, you can do it from the power bi desktop table itself

D you know how it looks when you export to csv a paginated report ?, i need to fetch the data per season to use a LLM chat and ask questions about the report, i can do it myself for a local report but i need to do with a automatics script

peya02 commented 6 months ago

I don't know about a paginated report.

CrasCris commented 5 months ago

I don't know about a paginated report.

Hi again, i figured out how to export data of visuals using powerbi javascript api, is a long process but it works fine for what i wanbt and change the powerbi capacity work the powerbi rest api using python, the code if you want to use it with flask, you have to make the 3 steps the blogs said, the csv format only works for paginated report

peya02 commented 5 months ago

Hello, and does it correctly export everything in the report or only what is seen? I mean that if in the report there is a very long table with scroll, the data that we say is hidden, does it also export it well?

CrasCris commented 5 months ago

The limit per visual objects is 30k rows, i follow this docs

peya02 commented 5 months ago

Well, when you have it published, let me know and I will try it to see how it goes. The truth would be very good for me.

CrasCris commented 5 months ago

`var embeddedReport; var models; let reportLoadedResolve, reportLoaded = new Promise((res, rej) => { reportLoadedResolve = res; }); let reportRenderedResolve, reportRendered = new Promise((res, rej) => { reportRenderedResolve = res; });

const heightOutput = document.querySelector("#height"); const widthOutput = document.querySelector("#width"); const deviceTypeOutput = document.getElementById('deviceType');

$(function () { var reportContainer = $("#report-container").get(0);

models = window["powerbi-client"].models; 
//MOBILE=MobilePortrait | DESKTOP=MobileLandscape
let permissions = models.Permissions.All;
var reportLoadConfig = {
    type: "report",
    tokenType: models.TokenType.Embed,
    permissions: permissions,
    settings: {
        layoutType: models.LayoutType.MobileLandscape 
    }
};

$.ajax({
    type: "GET",
    url: "/getembedinfo",
    data: {workspace_id:' ', // The parameters of the specific workspace and report
            report_id : ''
            },
    dataType: "json",
    success: function (data) {
        var embedData = $.parseJSON(JSON.stringify(data));
        reportLoadConfig.accessToken = embedData.accessToken;
        // You can embed different reports as per your need
        reportLoadConfig.embedUrl = embedData.reportConfig[0].embedUrl;

        // Embed Power BI report when Access token and Embed URL are available
        embeddedReport = powerbi.embed(reportContainer, reportLoadConfig); 

        // Triggers when a report schema is successfully loaded
        embeddedReport.on("loaded", function () {
            console.log("Report load successful");
            reportLoadedResolve();
        });
        // Triggers when a report is successfully embedded in UI
        embeddedReport.on("rendered", function () {
            console.log("Report render successful");
            reportRenderedResolve();
        });
        embeddedReport.off("error");
        embeddedReport.on("error", function (event) {
            var errorMsg = event.detail;
            console.error(errorMsg);
            return;
        });
    },
    error: function (err) {
    }
});

});

async function exportVisualData(visual) { try { const data = await visual.exportData(models.ExportDataType.Summarized, 1000); // limit of how many rows can export max is 30000 return { name: visual.name, title: visual.title, type: visual.type, data: data.data }; } catch (error) { console.error(Error exporting data for visual ${visual.type}:, error); return null; } }

async function sendDataToBackend(visualData) { try { await $.ajax({ type: "POST", url: /saviainfo/all, contentType: "application/json", data: JSON.stringify({ visuals: visualData }), success: function (response) { console.log(Data sent successfully); }, error: function (error) { console.error(Error sending data:, error); } }); } catch (error) { console.error(Error in sendDataToBackend:, error); } }

async function sendQuestionToBackend(question) { try { await $.ajax({ type: "POST", url: /saviaconsulta, contentType: "application/json", data: JSON.stringify({ questions: question }), success: function (response) { console.log(Question sent successfully); }, error: function (error) { console.error(Error sending question:, error); } }); } catch (error) { console.error(Error in sendQuestionToBackend:, error); } }

function reportWindowSize() { heightOutput.textContent = window.innerHeight; widthOutput.textContent = window.innerWidth;

// Check if height is greater than width
if (window.innerHeight > window.innerWidth) {
    deviceTypeOutput.textContent = "Mobile";
    newSettings = {
        layoutType: models.LayoutType.MobilePortrait
    };
    embeddedReport.updateSettings(newSettings);
} else {
    deviceTypeOutput.textContent = "Desktop";
    newSettings = {
        layoutType: models.LayoutType.MobileLandscape
    };
    embeddedReport.updateSettings(newSettings);
}

}

async function runReportOperations() { await reportLoaded; await reportRendered;

window.onload = reportWindowSize;
window.onresize = reportWindowSize;
//get data and sending to backend for SAVIA 
try {
    const pages = await embeddedReport.getPages();
    let page = pages.filter(function (page) {
        return page.isActive;
    })[0]; 
    const visuals = await page.getVisuals();

    // avoid this visuals to export
    const excludeTypes = ['slicer', 'card', 'shape', 'actionButton', 'bookmarkNavigator', 'textbox'];
    const filteredVisuals = visuals.filter(visual => !excludeTypes.includes(visual.type));

    const visualDataPromises = filteredVisuals.map(visual => exportVisualData(visual));
    const visualData = await Promise.all(visualDataPromises);

    // avoid null data 
    const filteredVisualData = visualData.filter(data => data !== null);

    console.log(filteredVisualData)
    await sendDataToBackend(filteredVisualData);

    var question = '¿Cual es la cuenta contable con mayor final compras y dime su valor ?';
    await sendQuestionToBackend(question);

} catch (errors) {
    console.log(errors);
}

}

runReportOperations(); `

This is the javascript code to send the data, it also have the logic to change with mobile or desktop view , you just need embedded the powerbi report as the docs with python using flask

peya02 commented 4 months ago

Hi friend,

I'm trying to adapt your code to my program and I get some errors. I'll send you my js code that I have to create the embed to see if you can help me to adapt it to yours and also be able to create the pdf.

$(function () {

    var reportContainer = $("#report-container").get(0);

    // Initialize iframe for embedding report
    powerbi.bootstrap(reportContainer, { type: "report" });

    var models = window["powerbi-client"].models;
    var reportLoadConfig = {
        type: "report",
        tokenType: models.TokenType.Embed,

        // Enable this setting to remove gray shoulders from embedded report
        // settings: {
        //     background: models.BackgroundType.Transparent
        // }
    };
    var idrepor = $("#idrepor").html();
    var rol = $("#rol").html();
    $("#idrepor").remove();
    $("#rol").remove();
    $.ajax({
        type: "GET",
        url: "/getembedinfo",
        data:{report:idrepor,
                rol:rol},
        dataType: "json",
        success: function (data) {

            embedData = $.parseJSON(JSON.stringify(data));
            reportLoadConfig.accessToken = embedData.accessToken;

            // You can embed different reports as per your need
            reportLoadConfig.embedUrl = embedData.reportConfig[0].embedUrl;

            // Use the token expiry to regenerate Embed token for seamless end user experience
            // Refer https://aka.ms/RefreshEmbedToken
            tokenExpiry = embedData.tokenExpiry;

            // Embed Power BI report when Access token and Embed URL are available
            var report = powerbi.embed(reportContainer, reportLoadConfig);

            // Triggers when a report schema is successfully loaded
            report.on("loaded", function () {
                console.log("Report load successful")
            });

            // Triggers when a report is successfully embedded in UI
            report.on("rendered", function () {
                console.log("Report render successful")
            });

            // Clear any other error handler event
            report.off("error");

            // Below patch of code is for handling errors that occur during embedding
            report.on("error", function (event) {
                var errorMsg = event.detail;

                // Use errorMsg variable to log error in any destination of choice
                console.error(errorMsg);
                return;
            });
        },
        error: function (err) {

            // Show error container
            var errorContainer = $(".error-container");
            $(".embed-container").hide();
            errorContainer.show();

            // Format error message
            var errMessageHtml = "<strong> Error Details: </strong> <br/>" + $.parseJSON(err.responseText)["errorMsg"];
            errMessageHtml = errMessageHtml.split("\n").join("<br/>")

            // Show error message on UI
            errorContainer.html(errMessageHtml);
        }
    });
});
CrasCris commented 4 months ago

What kind of error you got ? , i adapt the example code with flask to fastapi, see this REPO

peya02 commented 3 months ago

Well, it gives several errors, I guess that since I'm adapting your code to mine there will be things that are not correct. Regarding the example you mention, it's the one I used to start my application.

CrasCris commented 3 months ago

it should be a Sintaxys name in the variables , do you have the repo i can see the full code to help you ?

peya02 commented 3 months ago

I don't have the code uploaded, it's just that I'm also trying to configure the server in parallel to have the production part and the dev part of the project but I can't get the dev part to start, I use gunicorn + nginx

CrasCris commented 3 months ago

Ohhh i get it, i'll answer if you send or upload something dw

peya02 commented 2 months ago

One question, do you know how I can make a project with nginx & gunicorn but have two different URLs, one for production and one for dev, with the code being different of course. Let's see if now that I've returned from a few days I can try your code to see if I can get it working. Thanks.

CrasCris commented 2 months ago

Hmmm if you just have one server, you just need to change the ports that you will use, idk if gunicorn have something like that , or you can deploy them with docker

peya02 commented 2 months ago

I've been seeing the Docker option a lot lately and people say it's better. I understand that you can configure the same Docker for the different environments you want and assign a port to each environment. Is that correct?

CrasCris commented 2 months ago

I've been seeing the Docker option a lot lately and people say it's better. I understand that you can configure the same Docker for the different environments you want and assign a port to each environment. Is that correct?

Yep

peya02 commented 2 months ago

Hi, a quick question. I have the .env file for the environment variables, it turns out that I have modified some values ​​for the connection to the database but the application still uses the one that was there before. Do you know how to reload those values ​​in vs code (local machine)? Thanks.

CrasCris commented 2 months ago

You just need to reload the execution to take the new values

peya02 commented 2 months ago

That's what I was doing but it still took the old values, I added this line and now it takes them correctly. load_dotenv(override=True) I already had load_dotenv but without the override

Thanks!