Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Monday, March 18, 2024

Visual Studio Code Jupyter Python Extension line wrap in output cell - how to set notebook.output.wordWrap

My Visual Studio Code (Mac) Jupyter extension (plug-in) output was not line wrapped. It was a single unreadable output string. I had to either copy and paste into a markdown cell (where VSC line wrap config is followed) or I had to come up with Python output formatting solutions.

Then I saw a video in which the developer's cell block output was wrapping. So I went looking again. This turned out to be a lot harder than it was, say, 10 years ago. I spent at least 40 minutes, and probably more, digging through obscure corners of the internet

At last I got a hint. There is something called "notebook.output.wordWrap" that defaults to false but can be set true. If you can figure out how to set it.

I couldn't figure out how to set this value for the Jupyter Notebook extension on my Mac however. I got some hints from Microsoft's documentation on editing settings.json even though it's not correct about how to navigate to preferences. I used the ⌘, shortcut. From there I searched for "wordwrap"

You can see the result below. I needed to set Notebook > Output > Word Wrap to true. The copied JSON version of this is "notebook.output.wordWrap": true. You can see the setting in the screen shot below (click for full size).

I set this value for User and Workspace both, I assume Workspace wins but I don't know. Now my output wraps so I can read it.

There's also supposed be a metadata tag that causes a Jupyter notebook cell to scroll output but I couldn't get I too work and it felt too obscure to rely on.

Saturday, January 06, 2024

Rendering ChatGPT output in readable form in a Juypter Notebook

Update: This post is still useful, but there's also a way to enable line wrap in Visual Studio Code's Jupyter extension. You can also use the Python Print function instead of the Display example in my original post. For example:

output = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="List the days of the week: ",
    max_tokens=100,
    stop = "Saturday", #put this in for fun
)
print(output.choices[0].text)

Note these print parameters are specific to this particular object's structure. (I think JSON but I'm a newbie.)

Original below
--------

This was a bit of a revelation. I don't know Python but I've been working through a ChatGPT / LLM tutorial using Visual Studio Code and a Juypter Notebook on macOS. In a Jupyter cell the output renders below the cell and it looks like this:

Completion(id='cmpl-....', choices=[CompletionChoice(finish_reason='stop', index=0, logprobs=None, text='1...

All in one unreadable line with \n as a paragraph deliver and no line wrap.

I asked ChatGPT 4 to help. Over a series of interactions I tried different things and got various error messages I passed to ChatGPT 4. In turn it analyzed my error message and suggested fixes.

This is what I ended up with in about 15 minutes, here added to a cell that ran a simple prompt query

from IPython.display import display, HTML

output = client.completions.create(

    model="gpt-3.5-turbo-instruct",

    prompt="write me a poem",

    max_tokens=100,

    n=3

)

text_content = output.choices[0].text if output.choices else ""

html_output = text_content.replace('\n', '<br>')

display(HTML(html_output))

This is what the output looks like now (the poetry is greeting card quality and mildly painful):

A poem, a weave of words and rhyme

A tapestry of thoughts and time

A magic spell from the poet's pen

A story of love, of loss, of when


The stars above, they guide my hand

As I write of distant lands

Of fiery sunsets and ocean tides

Of moments we hold and let slip by ...   

[Adolescent poetry truncated] 

This screenshot shows it best ...


We are in a new world.

Wednesday, June 24, 2020

Python macOS environments for learners in 2020

My daughter is auditing Coursera's Intro to Python class. It's pretty standard stuff, but I was surprised by the development environment. For macOS there's a non-trivial Python install that requires some unix knowledge, use of the Homebrew package manager, dealing with admin vs non-admin user issues, consideration of pyenv, editing the path, and finally installing Python.

That's a long way from the ease of, say, TurboPascal circa 1983.

I figured there had to be a better way, but Google only found me some pro-level IDEs. It fell to Twitter to clue me in to the modern scene. The 4 good modern options turn out to be:
  • Google Colab: absolute easiest and least painful. I believe the Python code executes in the browser, so it's substantially slower than execution directly in macOS.
  • Microsoft Visual Studio Code for macOS: this does require the traditional Python install with Homebrew, but it's a very beginner friendly environment. The Python plugin provides Jupyter support.
  • Homebrew Jupyter: similar to Colab but like Visual Studio is part of the Homebrew/Python path.
  • Azure does Jupyter Notebooks (via @jhovland) at notebooks.azure.com.
Years ago I ran into iPython as a novice environment; turns out it morphed into Juypter.

It's a sign of the times that Google search didn't turn up a blog post with these options. (It won't find this one either, I'm way off Google's radar now.) Once I'd identified the above options however I could do a Google search to find an educational resource that did mention then:

There are many ways to write and execute Python code:

Python tutor (online, visual debugger)
Python interpreter (command line)
Visual Studio Code (editor, good debugger)
Jupyter (notebook)
Google Colab (online, collaborative)
 
During this lab we see all of them and familiarize with the exercises format. For now ignore the exercises zip and proceed reading.

That site is the University of Trento's data science lab course, updated 2019/2020.  The U of Trento was founded in 1962. Reading the wikipedia page it seems to have started out focusing on sociology (and, given the era, was likely a wee bit Left) but now seems to be very tech.

The course material is presumably translated from Italian. It's quite readable though it would benefit from a native speaker updating the GitHub content. Judging by my little test it may be one of the best resources of its kind.

See also:

I came back to Python for course on working with the OpenAI ChatGPT LLM. This time around I used Visual Studio Code with the Jupyter support. I use the default Python PIP package manager but I think Microsoft favors Conda. As of 2024 CoPilot is an option but it is not free.

Tuesday, October 20, 2009

The MobileMe Massacre begins

After several years of watching with vague disgust, I purchased a MobileMe 4 5 person family pack (via Macintouch referral link) from Amazon for $123. It took 12 days to arrive, which is about 3 times as long as I’d expected.

I’ll be using it for Emily, my mother (remote maintenance) and me. The sum of services that I think will make it worthwhile include:

  • iPhone locator, send message, lock and remote wipe services
  • Outlook 2003/2007 synchronization to MobileMe contacts, as used in my Contacts project. (The SyncWiz failure persuaded me to seek the only Outlook AddIn Contacts Sync solution I’ve had success with).
  • Webdav (iDisk) shares and large file messaging
  • Remote maintenance/access (esp... for my mother)

I don’t think I have any use for the Calendaring, bookmarks, email, or photo gallery services. I certainly don’t want to commit my data to MobileMe; Apple is to Data Freedom as the Birthers are to Obama.

I will have more to say about each of the features I use, which is best done in stages because MobileMe is a fairly amorphous and fluid set of services. Some of the capabilities are OS X specific, and some are probably 10.6 specific (or less buggy on 10.6). Some are iPhone specific (locator), some are iLife specific (albums, iWeb) and a few work with a PC (which I actually need).

In general the more Apple products you use, the more MobileMe becomes a reasonable purchase.

I’ll add links below to the next few weeks of reviews.

See also:

Update 11/7/09: I've discovered that even iCal is synchronizing even when it's disabled in MobileMe preferences. Obnoxious bug.



Monday, September 15, 2008

Stack Overflow: a brave new take on supporting software developers

I don’t code, but I’m a frequent customer of sites that claim to support software developers. I can vouch for Joel Spolsky’s critique of existing sites – and it’s a critique that applies to technical support sites in general. Lots of options, quality low, extinction rate high, spam attacks severe.

Between Jeff Atwood and Joel Spolsky and their friends and supporters, this one has a chance to be different. It uses a combination of private funding with a community wiki and voting/reputation management infrastructure.

I’ll be adding it to the top of my custom search engines (every software person needs a custom search engine). Here’s Joel on the service (emphases mine) …

Stack Overflow Launches - Joel on Software

You know what drives me crazy? Programmer Q&A websites. You know what I’m talking about. You type a very specific programming question into Google and you get back:

  • A bunch of links to discussion forums where very unknowledgeable people are struggling with the same problem and getting nowhere,
  • A link to a Q&A site that purports to have the answer, but when you get there, the answer is all encrypted, and you’re being asked to sign up for a paid subscription plan,
  • An old Usenet post with the exact right answer—for Windows 3.1—but it just doesn’t work anymore,
  • And something in Japanese.

If you’re very lucky, on the fourth page of the search results, if you have the patience, you find a seven-page discussion with hundreds of replies, of which 25% are spam advertisements posted by bots trying to get googlejuice for timeshares in St. Maarten, yet some of the replies are actually useful, and someone whose name is “Anon Y. Moose” has posted a decent answer, grammatically incorrect though it may be, and which contains a devastating security bug, but this little gem is buried amongst a lot of dreck….

Well, technology has gotten better since those discussion forums were set up. I thought that the programming community could do better by combining the idea of a Q&A site with voting and editing.

Would it work? I had no idea. And it looked like there was no way to find out, because everyone at Fog Creek was really busy so nobody had any time to build this.

Then, out of the blue, Jeff Atwood called me up. His own blog, Coding Horror, was starting to rack in the dough, and he was trying to figure out if that meant he could quit his day job and just blog.

Pattern-matching rules fired in my brain. The hardest thing about making a new Q&A site is not the programming—it’s the community. You need a large audience of great developers so you have the critical mass it takes to get started. Without critical mass, questions go unanswered and the site becomes a ghost town. I thought the combination of my audience (#15 on Bloglines) and Jeff’s (#89) would bring enough great developers into the site to reach critical mass on day one. So Jeff and I decided to go in together on this.

… After a very short, five-week private beta, we’re opening Stack Overflow to the public today…

… Every question in Stack Overflow is like the Wikipedia article for some extremely narrow, specific programming question. How do I enlarge a fizzbar without overwriting the user’s snibbit? This question should only appear once in the site. Duplicates should be cleaned up quickly and redirected to the original question.

Some people propose answers. Others vote on those answers. If you see the right answer, vote it up. If an answer is obviously wrong (or inferior in some way), you vote it down. Very quickly, the best answers bubble to the top. The person who asked the question in the first place also has the ability to designate one answer as the “accepted” answer, but this isn’t required. The accepted answer floats above all the other answers.

Already, it’s better than other Q&A sites, because you don’t have to read through a lot of discussion to find the right answer, if it’s in there somewhere.

Indeed, you can’t even have a discussion. A lot of people come to Stack Overflow, not knowing what to expect, and try to conduct a discussion when they should be answering the question. The trouble here is that answers are always listed in order of votes, not chronologically, so the discussion instantly becomes scrambled when the votes start coming in.

Instead, we have editing. Once you’ve earned a little bit of reputation in the system (and there are all kinds of ways to earn reputation), you can edit questions and answers….

… There are lots of good ways to edit things. You can improve spelling, grammar, and even copy edit any question or answer to make it better. After all, for the next 20 years, this question will be the canonical place on the web where programmers will come to find out about enlarging fizzbars without overwriting snibbits. Anything you can do to clarify, explain, or improve the question or the answer will be a public service. If there’s code in the answer, you can debug it, refactor it, or tweak it to make it better.

You can also improve on the answers. If an answer is incomplete, expand on it. If an answer has a bug in it or is obsolete, you can edit it and fix it. Because Q&A in Stack Overflow are editable, you can safely link to a Stack Overflow permalink knowing it will always have a good answer. Stack Overflow won’t have the problem of other sites where obsolete or incorrect answers have high Google PageRank simply because they’ve been on the Internet for so long. If someone finds a security bug in an answer, it can be fixed… it won’t keep coming up in Google’s results for years and years poisoning future code.

Want to know an easy way to earn reputation? Find a question somewhere with several good, but incomplete, answers. Steal all the answers and write one long, complete, detailed answer which is better than the incomplete ones. Sit back and earn points while people vote up your comprehensive answer.

In addition to voting on answers, you can vote on questions. Vote up a question if you think it’s interesting, if you’d like to know the answer, or if you think it’s important. The hot tab on the home page will show some of the highest-ranked recent questions using an algorithm similar to digg or Reddit. If you’re generally interested in programming and want to learn something new every day, visit the hot tab frequently.

Want to test your knowledge? Visit the Unanswered tab. Right now, you just see a list of questions with no answers (and there are very few), but in the near future, we’ll actually tailor the list to show you questions that we think you have a chance of answering, based on questions you’ve successfully answered in the past.

We have tags. Every question is tagged so, for example, if you’re a Ruby guru, you can ignore everything but Ruby and just treat Stack Overflow as a great Ruby Q&A site. A single question can have multiple tags, so you don’t have to figure out which single category it fits in best. Like everything else, the tags can be edited by good-natured individuals to help keep things sorted out neatly. And you can have a little fun: stick a homework tag on those questions where someone seems to be asking how to delete an item from a linked list…

… What kind of questions are appropriate? Well, thanks to the tagging system, we can be rather broad with that. As long as questions are appropriately tagged, I think it’s okay to be off topic as long as what you’re asking about is of interest to people who make software. But it does have to be a question. Stack Overflow isn’t a good place for imponderables, or public service announcements, or vague complaints, or storytelling.

I’m extremely excited about Stack Overflow. It’s fast and clean. It costs us practically nothing to operate, so we won’t need to plaster it with punch-the-monkey ads; we plan to keep it free and open to the public forever. And it might make it a little bit easier to be a programmer.