How can I use curses with PyCharm?

So, I've scoured the deepest lines of the internet and didn't seem to find a solution for my problem. I am using PyCharm Community Edition 2017.1.1 on macOS 10.12.4 and using curses in the following code with Python 3.6.1.

#!/usr/local/bin/python3
import sys
import urllib.request
import json
import time
from datetime import datetime
from datetime import timezone
import curses
import ssl

class Stock:
    def stockFromYahooWebService(ticker):
        url = "https://finance.yahoo.com/webservice/v1/symbols/{}/quote?format=json&view=detail".format(ticker.upper())
        user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1"

        urlrequest = urllib.request.Request(url, data=None, headers={"User-Agent": user_agent})

        ssl._create_default_https_context = ssl._create_unverified_context

        urlcontent = json.loads(urllib.request.urlopen(urlrequest).read().decode())
        fields = urlcontent['list']['resources'][0]['resource']['fields']    
        return fields
stdscr = curses.initscr()
curses.cbreak()
stdscr.keypad(1) 
try: 
    if len(sys.argv) == 2:
        ticker = sys.argv[1]
        while ('true') :
            stdscr.erase()
            fields = Stock.stockFromYahooWebService(ticker) 
            utctime = datetime.strptime(fields['utctime'], "%Y-%m-%dT%H:%M:%S+%f")
            localtime = utctime.replace(tzinfo=timezone.utc).astimezone(tz=None)
            localtimeString = localtime.strftime("%I:%M%p %Z %m/%d/%y")
            stdscr.addstr(0, 0, "{} ({})".format(fields['issuer_name'], fields['symbol']))
            stdscr.addstr(1, 0, "Price: ${:,.2f}".format(float(fields['price'])))
            stdscr.addstr(2, 0, "Change: {:+,.2f} ({:+,.2f}%)".format(float(fields['change']),float( fields['chg_percent'])))
            stdscr.addstr(3, 0, "Volume: {:,}".format(int(fields['volume'])))
            stdscr.addstr(4, 0, "Last Trade: {}".format(localtimeString))
            stdscr.refresh()
            time.sleep(5)
finally:
    curses.nocbreak()
    stdscr.keypad(0)
    curses.echo()
    curses.endwin()
    if len(sys.argv) != 2 : 
        print('Usage: ./rtquote.py <ticker>')

      

I know that PyCharm uses its own terminal to output the output, and I cannot figure out how to set it up to make my program work in PyCharm. I can successfully run my program on the Mac terminal, however I want to be able to do this inside PyCharm.

I humbly appeal to you, good strangers of the Internet, for your help. Please let me know if it is possible what I am trying to do in PyCharm, and if so, how can it be done.

I am getting the following error in the PyCharm terminal window:

)07[?47h[1;24r[m[4lTraceback (most recent call last):
  File "/rtquote.py", line 24, in <module>
    curses.cbreak()
_curses.error: cbreak() returned ERR

Process finished with exit code 1

      

+3


source to share


1 answer


I solved this problem by adjusting the working parameters:



  • click the Run menu , then Change Configurations ...
  • select the configuration appropriate for your project.
  • check the box Emulate terminal in output console
0


source







All Articles