Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
LegendaryTokash
Frequent Visitor

Python formatting for streaming dataset -formatting question

Alright, so I am not a developer by trade, but a windows infrastructure admin.  I dabble in electronics and automation, and have been doing odd experiments with Rpi and Arduino components for a couple of years now.   Only recently have I explored taking the realtime data I can collect and bundling it together for a practical purpose - and am exploring PowerBI as a method to visualize.

 

Long story short, I have a Rpi pulling data from an Arduino, temp, humidity, voltage and current information.  I'm following this guide on how to get started, and I am failing to understand the python formatting being done.  

https://powerbi.microsoft.com/en-us/blog/using-power-bi-real-time-dashboards-to-display-iot-sensor-d...  -- the only difference is i am not rewiring existing projects to read directly from the Pi, but using existing Python via Serial, to get my values.

Without taking a large number of hours to formally learn python, I have a moderate understanding of its syntax and controls, but I am struggling to understand and adapt this line:

 

# data that we're sending to Power BI REST API
data = '[{{ "timestamp": "{0}", "temperature": "{1:0.1f}", "humidity": "{2:0.1f}" }}]'.format(now, temp, humidity)

 

I need to understand how to replace the formatting components with a string and a float variable similar to below.  Ideally I'd like to better understand the formatting function, while I don't necessarily need it here, I am sure it will be useful in the future.  I've already populated the variables, and in my current iteration, pump that data into a mySQL database.   I am trying to eliminate that component overall, by leveraging streaming dataset instead.  Maybe it would be easier for me to understand this line with similar formatting, before i try and adjust the one used with the REST API.

 

print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temp, humidity)

 

 

 

Can someone give me a mid-level breakdown of what the formatting components of this line are?

1 ACCEPTED SOLUTION
Eric_Zhang
Employee
Employee


 

print 'Temp={0:0.1f} Humidity={1:0.1f}'.format(temp, humidity)

 


@LegendaryTokash

That formating is actually Python syntax specific. You would get more prompt response when asking in a Python community.

in the { }, the 0,1 before ":" are the the placeholder, which would be replaced with the arguments in format(temp, humidity). Regarding the 0.1f after ":", f stands for float type, 1f stands for the precision after decimial point, those 0.1f would be used to format the passed temp and humidity. For example, say temp=10, humidity=10.11, the print output would be like 

Temp=10.0, Humidity=10.1

 

For more details, you can check 

How does this formatting code work?

Python format samples

 

The stream dataset doesn't care from what language the data sent API is called, as long as the data json in the required format. If you have any question on Power BI, feel free to post. For any further question on Python, I'd suggest you post it in the Python community, as there're more Python experts.

View solution in original post

1 REPLY 1
Eric_Zhang
Employee
Employee


 

print 'Temp={0:0.1f} Humidity={1:0.1f}'.format(temp, humidity)

 


@LegendaryTokash

That formating is actually Python syntax specific. You would get more prompt response when asking in a Python community.

in the { }, the 0,1 before ":" are the the placeholder, which would be replaced with the arguments in format(temp, humidity). Regarding the 0.1f after ":", f stands for float type, 1f stands for the precision after decimial point, those 0.1f would be used to format the passed temp and humidity. For example, say temp=10, humidity=10.11, the print output would be like 

Temp=10.0, Humidity=10.1

 

For more details, you can check 

How does this formatting code work?

Python format samples

 

The stream dataset doesn't care from what language the data sent API is called, as long as the data json in the required format. If you have any question on Power BI, feel free to post. For any further question on Python, I'd suggest you post it in the Python community, as there're more Python experts.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.