Having trouble playing music using IPython - python

I have the lines of code
import IPython
IPython.display.Audio(url="http://www.1happybirthday.com/PlaySong/Anna",embed=True,autoplay=True)
And I'm not really sure what's wrong. I am using try.jupyter.org to run my code, and this is within if statements. The notebook is also taking in user inputs and printing outputs. It gives no error, but just doesn't show up/start playing. I'm not really sure what's wrong.
Any help would be appreciated. Thanks!

First you should try it without the if statement. Just the two lines you mention above. This will still not work, because your URL does point to an HTML page instead of a sound file. In your case the correct URL would be 'https://s3-us-west-2.amazonaws.com/1hbcf/Anna.mp3'.
The Audio object which you are creating, will only be displayed if it is the last statement in a notebook cell. See my Python intro for details. If you want to use it within an if clause, you can use IPython.display.display() like this:
url = 'https://s3-us-west-2.amazonaws.com/1hbcf/Anna.mp3'
if 3 < 5:
IPython.display.display(IPython.display.Audio(url=url, autoplay=True))
else:
print('Hello!')

Related

VSCode: show entire variable in debug console

when debugging python code in VSCode, how do i show the entire variable. As you can see from the image below, i am printing out the variable 'x'..but not all of it's values shows in the debugging console. it is cut off and followed by an elipse indicating there is more:
While I know I can use the '>' to show a list of each item in the set in this case, is there a way to print the entire variable as a string in the debug pane/console - much like we would be able to if we were using the command line for our venv?
for item in set(x):
print(item)
You should be able to click the > sign to the left of the variable result to expand it.
I made a simple replay. You can find the variable on the left side after using debug:
You can copy it like the following picture:
you can paste the copied content:

How do you avoid errors do an automatic grading script?

This is my second beginner python course, so I'm learning! For most things I'm working with Python notebooks because I feel it has more flexibility. The actual program is Coursera. I recently transferred the following code from python notebooks (that worked fine) back to Coursera, and I'm getting syntax errors. Not the first time. How do I avoid this? Looking for any advice.
"TabError: inconsistent use of tabs and spaces in indentation"
def email_list(domains):
emails = []
for provider, user in domains.items():
for each_user in user:
new_user=("{}#{} ".format(each_user,provider))
emails.append(new_user)
return(emails)
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], "yahoo.com": ["barbara.gordon", "jean.grey"], "hotmail.com": ["bruce.wayne"]}))
I have faced such problems in the past and here is what I did, after pasting your code in Coursera, try deleting every space and tab from your code and retype them for example, delete the spaces in the def line until you have this line
def email_list(domains):emails = []
after this line pressing return or enter right after the colon will give you the right indentation and hopefully, your code will run smoothly.

Selenium in Jupyter Notebook, Different Outcome on Different Cells

I'm a student studying Python and Web Crawling in Korea.
I found something I can't understand why. I want to ask why this happens and how can I fix it.
It will lovely if someone is gonna help me.
Here is my situation:
This is a code for my web crawling. There is some Korean words, but that's not important, I think.
zeropay_official = 'https://www.zeropay.or.kr/main.do?pgmId=PGM0081'
driver = webdriver.Chrome('./driver/chromedriver')
driver.get(zeropay_official)
driver.find_element_by_id('tryCode').click()
driver.find_element_by_id('tryCode').send_keys('서울특별시')
driver.find_element_by_id('skkCode').click()
driver.find_element_by_id('skkCode').send_keys('노원구')
driver.find_element_by_id('pobsAfstrName').send_keys('다마식당')
driver.find_element_by_xpath('//*[#id="form"]/div[2]/a').click()
test = driver.find_element_by_id('list_div')
test.text
and right below this Jupyter Notebook cell, I put the last line of the code,
test.text
to check what's happening.
But, first cell's output ls ''(None), and second cell's output is some string which I wanted to get.
Why Is this happening? And if I need to get the output data string on the first cell, to make this code as a module so my team can import it, what should I do?
Check this image if you couldn't clearly understand what I said due to my poor English.(sob)
You can add some wait time.
zeropay_official = 'https://www.zeropay.or.kr/main.do?pgmId=PGM0081'
driver = webdriver.Chrome('./driver/chromedriver')
driver.get(zeropay_official)
driver.find_element_by_id('tryCode').click()
driver.find_element_by_id('tryCode').send_keys('서울특별시')
driver.find_element_by_id('skkCode').click()
driver.find_element_by_id('skkCode').send_keys('노원구')
driver.find_element_by_id('pobsAfstrName').send_keys('다마식당')
driver.find_element_by_xpath('//*[#id="form"]/div[2]/a').click()
time.sleep(time_in_seconds)
test = driver.find_element_by_id('list_div')
test.text
As Korean text is taking some time to appear.

In SikuliX, type(variable) always types the variable then presses enter automatically. How do I avoid pressing enter?

Sorry if this is really basic, I cannot find a workaround. I have a variable called doc that stores the number 510 that was copied from an excel cell.
I need to type it in a field, but I need to continue typing in another field on the same page afterwards.
My code has:
type(doc)
The log shows:
[log] TYPE "510#ENTER."
The full code looks like this:
type(doc)
wait(1)
type(Key.DOWN)
type(Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE)
wait(1)
type(code)
However, I can't get to the type(code) because it switches page before I get there...
Using paste() maybe solved your issue here but this is not the right way to do that as Sikuli does not automatically presses any buttons.
Your problem is probably with the doc variable itself. In your case, you probably just copied the new line character with your variable from excel and that's why Sikuli is hitting Enter. To avoid that, try stripping the new line from your variable prior to typing it, like this:
doc.rstrip()
Then do your usual type(doc) and it should be fine.
Another thing that works is: doc.strip()
It turns out sikuli writes /n after strings, so strip removes that /n.

Why does IPython notebook only output one DIV from this code?

In an IPython notebook I input this code in a cell:
from IPython.display import HTML
HTML("""<div>One</div>""")
HTML("""<div>Two</div>""")
How come the output cell only contains the second div?
EDIT. #Dunno has shown how I can put all the html into one HTML() and both elements are rendered, but I still don't understand what's going on. Here's a more general case:
When I enter this in an input cell:
1
2
3
The output is
3
But if I enter the following:
print 1
print 2
print 3
Then I get this output:
1
2
3
What's the difference? Is IPython notebook only evaluating the last statement when I don't use print statements? Or is each subsequent evaluation overwriting the previous one?
Yeah I found some documentation on this, and HTML is actually a class, not a function.
So the correct code would be
from IPython.display import HTML
myhtml = HTML("""<div>One</div><div>Two</div>""") #make the html object
myhtml #display it
Now it makes sense why your code displays only one div.
To display multiple parts create multiple variables, containing the html and then concatenate them inside one call to HTML.
div1 = """<div>One</div>"""
div2 = """<div>Two</div>"""
myhtml = HTML(div1 + div2)
myhtml
Edit:
I opened a ticket on ipython's github profile to see if it's a bug or a feature that only the last line of statements is displayed. Turns out, it's planned behaviour:
quoting Thomas Kluyver:
This is deliberate, because if you call something in a for loop that returns a value:
for line in lines:
f.write(line) # Returns number of bytes written
You probably don't want to see all those numbers in the output.
The rule in IPython is: if the last statement in your code is an expression, we display its >value. 1;2 is a pair of statements, whereas 1,2 is one statement, so both values will >display.
I hope that explains things a bit. We're happy to revisit decisions like this, but it's >been this way for years, and I don't think anyone has taken issue with it.
Just a small addition to #Dunno's answer: if you want to display multiple IPython.display.DisplayObject objects (this includes HTML objects but also images etc.) from a single cell, you can use the IPython.display.display function.
For example, you can do:
from IPython.display import HTML, Image, display
display(HTML("""<div>One</div>"""))
display(HTML("""<div>Two</div>"""))
display(Image("http://cdn.sstatic.net/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a", format="png"))

Categories