1

I am making a personal Live NHL stats program. Currently I have an output of

______________________________________________
Buffalo Sabres 0
Edmonton Oilers 0
______________________________________________
Toronto Maple Leafs 0
Calgary Flames 0
______________________________________________
Carolina Hurricanes 0
Philadelphia Flyers 0
______________________________________________

To get the home and away goals I am parsing NHLs JSON API like this.

   goals_home = jsonResponse["liveData"]['linescore']['teams']['home']['goals']

Right now when I run the program it just keeps repeating itself every 3 seconds without clearing the screen and looks messy. Is there a method to just clear the numbers and just repeat those in the same spot.

  • How are you displaying the output? Using `print()`? – nofinator Nov 12 '21 at 17:11
  • print(str(home) + " " + str(goals_home)) – AlexLikesWater Nov 12 '21 at 17:12
  • Generate HTML snd refresh. See https://stackoverflow.com/questions/16399355/refresh-a-local-web-page-using-python – balderman Nov 12 '21 at 17:13
  • You can try curses library for python. Probably needs some learnings though. – Hitobat Nov 12 '21 at 17:14
  • If you're just writing the scores to stdout with `print()`, see https://stackoverflow.com/q/37071230/245915 – nofinator Nov 12 '21 at 17:15
  • @balderman If you generate the HTML yourself, there's no need for Selenium, you can refresh it using JS. However generating HTML would require learning to use a web server (Flask or similar) and there are easier alternatives if OP is content with just terminal output. – Jan Pokorný Nov 12 '21 at 17:33

2 Answers2

0

You can use escape characters to "go up a line", after which you will overwrite what's already printed. You just need to know how many lines you printed. Also be aware this might not work in every terminal.

print("\033[F" * num_lines, end="")

If you want to do something more complicated, though, I recommend finding a dedicated library.

Jan Pokorný
  • 1,418
  • 15
  • 22
0

If you're using print(goals_home) to display the output, perhaps try print(goals_home, end='\r'). This end will replace the default new line, so that every time it prints, it will overwrite the previous line.

UdonN00dle
  • 723
  • 6
  • 28