Pode.Web
Pode.Web copied to clipboard
Execution in Order visible
Question
how to make action execution visible? If I click a button to execute a script, the script is executed but the execution is hidden. If in the intervals of execution there is an update of some text fields, for example, it does not matter where at the beginning, in the middle or at the end, I will see all this only after the complete execution of the script. no fields are updated during execution.
Kind of hacky but it you return from the script block multiple times and re-execute it you can do it:
Start-PodeServer -Threads 3 {
Add-PodeEndpoint -Address localhost -Port 8092 -Protocol Http
Enable-PodeSessionMiddleware -Secret 'ZZZ' -Duration (60 * 60 * 24) -Extend
Use-PodeWebTemplates -Title 'Tool'
$InfoSection = New-PodeWebCard -Content @(
New-PodeWebButton -Id 'But' -Name 'But' -ScriptBlock {
Start-Sleep -Seconds 0.5
$WebEvent.Session.Data.Progress += 5
if ($WebEvent.Session.Data.Progress -le 100) {
Update-PodeWebProgress -Value $WebEvent.Session.Data.Progress -Name 'Download'
Invoke-PodeWebButton -Id 'But'
} else {
Show-PodeWebToast -Message 'Download complete'
$WebEvent.Session.Data.Progress = 0
}
}
New-PodeWebProgress -Name 'Download' -Value 0 -Colour Green -Striped -Animated
)
Add-PodeWebPage -Name 'Page1' -Icon 'cart-minus' -Layouts $InfoSection
}
I see on the roadmap: Everything uses AJAX at present, if we move to using WebSockets then Output actions can happen on the fly
Hallo @ili101,
Message: Die Eigenschaft "Progress" wurde für dieses Objekt nicht gefunden. Vergewissern Sie sich, dass die Eigenschaft vorhanden ist und festgelegt werden kann.
StackTrace: bei System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
bei Invoke-PodeScriptBlock(Closure , FunctionContext )
bei System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
bei System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
bei System.Management.Automation.CommandProcessorBase.Complete()
I don't quite understand what you want to show. I also use Progressbar but it doesn't show progress update/change although I add progress update after certain task but still I get only end result and full progressbar at once.
The error is probably as you missing the Enable-PodeSessionMiddleware
line.
The general idea is as it can't be done correctly, split your task into steps, in every step update the progress and execute the next step. It works but it's hard to implement and can brake potentially so I don't recommend it.
The best solution is as listed in Badgerati roadmap is to use WebSockets, I don't know how big of a task it is.
I new see that there is a timer in Pode.Web New-PodeWebTimer
.
So in the script that executing the task we can save the progress into a Pode state. And set a timer to update the progress bar every 10 seconds from the state value:
Start-PodeServer -Threads 3 {
Add-PodeEndpoint -Address localhost -Port 8092 -Protocol Http
Enable-PodeSessionMiddleware -Duration (60 * 60 * 24) -Extend
Use-PodeWebTemplates -Title 'Tool'
$InfoSection = New-PodeWebCard -Content @(
New-PodeWebButton -Id 'Do' -Name 'Do' -ScriptBlock {
Set-PodeState -Name ("Progress" + $WebEvent.Session.Id) -Value 0
$Tasks = 1..200
for ($i = 0; $i -lt $Tasks.Count; $i++) {
Start-Sleep -Seconds 1
Set-PodeState -Name ("Progress" + $WebEvent.Session.Id) -Value (($i + 1) / $Tasks.Count * 100)
}
}
New-PodeWebProgress -Name 'Download' -Value 0 -Colour Green -Striped -Animated
New-PodeWebTimer -Interval 10 -ScriptBlock {
$Progress = Get-PodeState -Name ("Progress" + $WebEvent.Session.Id)
Update-PodeWebProgress -Value $Progress -Name 'Download'
}
)
Add-PodeWebPage -Name 'Page1' -Icon 'cart-minus' -Layouts $InfoSection
}
Hey @eugen257, @ili101,
Timers and State is the usual approach I tell people to use - or Files in place of State. And like how you've mentioned, have a button/etc update the State, and the Timer retrieve this to update elements dynamically. It's not elegant, but about the best way to achieve it.
As you've spotted @ili101, I do want to look at moving over to WebSockets instead of using AJAX which would make this a whole lot simpler!