Chris Padilla/Blog
My passion project! Posts spanning music, art, software, books, and more. Equal parts journal, sketchbook, mixtape, dev diary, and commonplace book.
- Research and gather reference
- Study in context (breaking down the construction of the object)
- Practice (playing with the form, mixing and matching elements)
- Imitate
- Assimilate
- Innovate
- Read documentation/forum posts/books
- Build a few sample apps
- Stray off and build a more complex, custom app
- Install Pytest through pip.
- When running your tests, call it through the command line:
- Write your test file!
- Playing with shapes and sound, their interaction with one another, is fun unto itself
- Creation is largely puzzle solving. Even playing piano is a finger puzzle
- There's no better way to express love for the world than to try to recapture it as you see it
- Progress, itself, is a joy! Learning an instrument more intimately or refining a line stroke, they all lead to small victories
- There are infinite paths to explore. None right or wrong, only guided by your curiosity and interest
COLOR!! π
My sister very kindly loaned me her Copics to play with!
After months in greyscale, it's fun to have shades and hues to work with!
Perfect opportunity to do this Satsuki study from My Neighbor Totoro! Borrowed from this beautiful Miyazaki art book.
From MVC to API Routes in ASP.NET Core
MVC patterns make for quick and easy development in .NET Core. This week, I'm switching over to an API servicing a React application, and there are a couple of small differences in getting started with serving up API endpoints:
ApiController Attribute
In MVC, all we needed to setup a controller was inherit from the controller class:
namespace BulkyBookWebDotNet6MVC.Controllers
{
public class CategoryController : Controller
{
. . .
}
}
For API's, we need to go up the family tree to ControllerBase
, which controller inherits from:
namespace BulkyBookWebDotNet6MVC.Controllers
{
public class CategoryController : ControllerBase
{
. . .
}
}
Attributes
In addition, we need a few different attributes:
[ApiController]
[Route("[controller]")]
public class BookController : ControllerBase
{
. . .
}
[ApiController]
will give goodies specific to APIs, such as specifying routes and more detailed error responses.
Since we have to specify routes, we're doing so with the next attribute [Route("[controller]")]
We're defaulting to the controller based routing, but we could just as well define our own route such as [Route("/users")]
Return
Of course, since we're working with an API returning data and not a View from an MVC, the return methods will be slightly different. [ApiController]
provides methods that are analogous to Status codes:
if (BookFromDb == null)
{
// Status: 404
return NotFound();
}
// Status: 200 with created object
return Ok(BookFromDb);
And that should be enough to get started! More details are available on MSDN.
C Fingerstyle Barre Improv
Just messing around today!
Shapely Dogs
Dependency Injection and Logging in .NET
Buy in large, server logs come out of the box with .NET Core. Setting up logging for you controllers, however, takes just a little bit of setup.
In console applications, logging is straightforward:
System.Console.WriteLine("Hello World");
In a .NET application, though, you'll likely need to pipe your logs through the logger class from your Dependency Injection.
Here's a look at a bare bones Controller:
using Microsoft.AspNetCore.Mvc;
namespace Bookshelf_cs.Controllers;
using Bookshelf_cs.Data;
using Bookshelf_cs.Models;
public class AuthorController : ControllerBase
{
private readonly ILogger<AuthorController> _logger;
public AuthorController(ILogger<AuthorController> logger)
{
_logger = logger;
}
}
In the constructor, we're constructing our controller with a _logger
attribute.
An aside on Dependency Injection
So why is this better than just instantiating a logger class directly? Like so:
private readonly ILogger<AuthorController> _logger = new Logger();
The short answer is to avoid tight coupling. By using the ILogger
interface, you can swap out the default Microsoft logger with a custom logger so long as it shares the same interface. More details with examples on MSDN.
That swapping out happens at your app building stage in your Program.cs file. If we wanted to customize the logger, it would look like this:
// Program.cs
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
builder.Logging.AddConfiguration(configuration.GetSection("Logging"));
var app = builder.Build();
Very DRY. If you need to make changes, it's all done at the application level. From there, the builder injects your logger of choice into your classes that accept a logger in their constructors.
Logging
Once it's instantiated in your class, you can log directly from the _logger
attribute:
_logger.LogInformation(1001, "Author passed");
_logger.LogWarning(1001, obj.ToString());
For any custom changes for what level of logs are displayed, you can tweak the appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.AspNetCore.SpaProxy": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
How To Make (Anything) In 3 Steps
I came across Ctrl + Paint recently, another great and nearly free resource for learning to draw.
They have a fantastic video on how to draw anything in a few steps:
The steps are:
The glossed over step 0 is to understand the foundations: "How to render form and light." No small step! But, once you have the vocabulary in place, that's how you handle drawing something new from imagination.
A recurring theme on ol' Chris D Padilla dot com is that everything is the same! Here, for example, is how Clark Terry talks about learning to play and be creative in jazz:
And hey, here it is for developing in a new programming language:
The trick behind it, and why the title of Ctrl + Paints video skews just a smidge click-baity, is that each step takes time. The reward, though, is that when you're comfortable with this process, you literally can make just about anything. No need to know everything before starting a project, and no need to master everything right away. Just follow the cycle, and iterate from there.
Owls
Speer - Baroque Dance
So stately! π©
Testing with Pytest
A quick start guide:
python3.7 -m pytest .
There's documentation for calling it globally, but I had no luck. Calling it through the same version of python with the -m
flag did the trick for me.
import pytest
@pytest.fixture(scope='class')
def my_tests_init(request):
# Set up DB connection here
try:
# Insert any sample date
except Exception as e:
# Handle exceptions
yield # Your tests will happen here
# Tear down: remove sample data from db.
# Close DB
Pytest will use decorators to add in the functionality needed to interpret your file correctly.
All your tests will live in a class like this one:
@pytest.mark.usefixtures("my_tests_init")
class TestMyClass:
def test_my_method(self):
expected = [{'cool': True}]
result = self.my_class.my_method()
assert result == expected
Here, the decorator ensures the the init method runs before and after these tests.
From there, test methods must start with "test" in the method name.
And you do your thing from there!
Pytest, unlike testing libraries like unittest or Jest in JS, doesn't use assertion methods. Here we just have the assert
keyword being used against a boolean expression. Even if we use a reference type, Pytest will know to assert the values of the dictionary or list deeply.
Additionally: If you're not finding what you need from the command line error logs, adding the v
flag will give you a closer look, while vv
gives you even more details. vv
Goes as far to show a git-like comparison, line by line, of what conflicts between your expected and actual results!
Sketchbook No. 5 Down!
Another one down!!
Started off very exercise heavy. These are figure study simplifications of the torso here:
Explored a few animal charicatures:
And then finished the book out with meditative automatic drawing:
My next one is a bigger sketchbook, so I'm excited to have more room to play. :)
Automatic Drawing
Something I'm still learning is to keep more play in all of my creative practices.
Maybe that's surprising to anyone looking in from what I share! But it's true β it's still a challenge to turn off the critical brain when I'm playing an instrument or drawing.
With drawing, I was just doodling and messing with shape at the start. Lately, I've caught myself leaning more towards filling all of my drawing time with exercises and course. If I'm aiming for the 50% rule, I'm really only hitting 25% play. Not entirely a bad thing, but it leaves little room for experimentation and just enjoying the act of drawing.
An experiment I'm trying out to really loosen up is automatic drawing, as demonstrated by Tim Gula here:
The end result is beautiful, but the result isn't really the point. Its meditation for the mind, playing purely with shape, line, and values.
The irony is that, in messing around, a lot of growth happens at the same time: Line quality improves, control over shape develops, and a relaxed brain allows for those periods of focus to be more energized.
Most exciting for me, though, is that when I'm just doodling outside of any coursework, I can shrug off the pressure of it having to be impressive. There's room for experimentation and simply enjoying putting pencil to paper!
I'm trying this out with music too. Lately, I've been spending all my piano and guitar time playing out of books, leaving no time to write or improvise. So now I have a notebook for jotting down improvised chords, melodies, and mini-tunes. Not with the goal of making another album. Just for the space to enjoy sound unto itself.
At the end of the day, sound and line are purpose enough.
Winston Churchill on Painting as a Pastime
I don't always find the inspiration I'm looking for from the bios and stories of full time artists. Those select few who can spend most of their week in the studio. Winston Churchill, who was surprisingly an enthusiastic, self proclaimed hobby painter, can speak much more clearly to the situation of having to find time to be creative in the nooks and crannies of the day.
I've been rattling in my mind all the different reasons I like drawing/piano/guitar/writing music/blogging, and why someone else might like to do them too.
To Churchill, it was no contest comparing painting to more typical leisure activities:
Even at the advanced age of Forty! It would be a sad pity to shuffle or scramble along through one's playtime with golf and bridge, pottering, loitering, shifting from one heel to the other, wondering what on earth to do β as perhaps is the fate of some unhappy beings β when all the while, if you only knew, there is close at hand a wonderful new world of thought and craft, a sunlit garden gleaming with light and colour of which you have the key in your waistcoat pocket. Inexpensive independence, a mobile and perennial pleasure apparatus, new mental food and exercise, the old harmonies and symmetries in an entirely different language, an added interest to every common scene, an occupation for every idle hour, an unceasing voyage of entrancing discovery β these are high prizes.
A particularly sobering passage acknowledges that, as a hobbyist, you won't reach the same great heights of technique as the life-long, full time painters. It's a problem I find myself wrestling with frequently, but Churchill brings it up so matter-of-factly and without much concern:
But if, on the contrary, you are inclined β late in life though it be β to reconnoitre a foreign sphere of limitless extent, then be persuaded that the first quality that is needed is Audacity. There really is no time for the deliberate approach. Two years of drawing-lessons, three years of copying woodcuts, five years of plaster casts β these are for the young. They have enough time to bear. And this thorough grounding is for those who, hearing the call in the morning of their days, are able to make painting their paramount lifelong vocation. The truth and beauty of line and form which by the slightest touch or twist of the brush a real artist imparts to every feature of his design must be founded on long, hard, persevering apprenticeship and a practice so habitual that it has become instinctive. We must not be too ambitious. We cannot aspire to masterpieces. We may content ourselves with a joy ride in a paintbox. And for this Audacity is the only ticket.
It's still worth the pursuit all the same. The rewards for that Audacity are many:
I have written in this way to show how varied are the delights which may be gained by those who enter hopefully and thoughtfully upon the pathway of painting; how enriched they will be in their daily vision, how fortified in their independence, how happy in their leisure. Whether you feel that your soul is pleased by the conceptions or contemplation of harmonies, or that your mind is stimulated by the aspect of magnificent problems, or whether you are content to find fun in trying to observe and depict the jolly things you see, the vistas of possibility are limited only by the shortness of life. Every day you may make progress. Every step may be fruitful. Yet there will stretch out before you an ever-lengthening, ever-ascending, ever-improving path. You know you will never get to the end of the journey. But this, so far from discouraging, only adds to the joy and glory of the climb.
So beautifully said. It all rings true for me:
And why wait? The time to plant a garden is now:
Try it, then, before it is too late and before you mock at me. Try it while there is time to overcome the preliminary difficulties. Learn enough of the language in your prime to open this new literature to your age. Plant a garden in which you can sit when digging days are done. It may be only a small garden, but you will see it grow. Year by year it will bloom and ripen. Year by year it will be better cultivated. The weeds will be cast out. The fruit-trees will be pruned and trained. The flowers will bloom in more beautiful combinations. There will be sunshine there even in the winter-time, and cool shade, and the play of shadow on the pathway in the shining days of June.
While the earlier, the better for overcoming the initial hurdle of learning the vocabulary, it's never too late. Thankfully, the barrier to entry is not technique or years of experience. Just Audacity!
Faber - This One is About Cats
The little rascals! π
Hippo!! π¦
Happy as a hippo with how this guy turned out!
Not going to lie, I can see the difference of spending nearly 6 months on lines with the Proko course!
MVC in ASP.NET Core
This week's edition of ASP.NET adventures: kick starting an MVC app with ASP.NET Core!
On the front-end side of things: Two things that make this a really delightful and smooth process are Razor Pages and Tag Helpers.
My MVC experience up to this point has been with templating engines in Express. What they do well is make it dead simple to get data into the template. The limitation is flexibility and reactivity. (Especially coming from working mostly in React.)
ASP.NET MVC apps have a really nice in between through these two tools.
I'll show the setup with Models and Controllers first, then on to views in action!
(Code samples and all of my info here are coming from this great introductory video to MVC in ASP.NET Core 6)
Models
Here's a look at the model I'm using:
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace BulkyBookWebDotNet6MVC.Models
{
public class Category
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[DisplayName("Display Order")]
[Range(1, 100, ErrorMessage = "Display Order must be between 1 and 100")]
public int DisplayOrder { get; set; }
public DateTime CreatedDateTime { get; set; } = DateTime.Now;
}
}
Nothing fancy! The code in square brackets are used by Entity Framework for field validation. DisplayName
will affect what's rendered in the view, for example. Otherwise DisplayOrder
would be the defaut.
Controller
Setting up controllers is a straight-shot. Grab what's needed from the database and send it the view.
// GET: /<controller>/
public IActionResult Edit(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
var CategoryFromDb = _db.CategorySet.Find(id);
//var CategoryFromDb = _db.CategorySet.FirstOrDefault(u=>u.Id == id);
//var CategoryFromDb = _db.CategorySet.SingleOrDefault(u=>u.Id == id);
if (CategoryFromDb == null)
{
return NotFound();
}
return View(CategoryFromDb);
}
// Post: /<controller>/
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(Category obj)
{
if (obj.Name == obj.DisplayOrder.ToString())
{
ModelState.AddModelError("name", "Display order cannot match the name.");
}
if (ModelState.IsValid)
{
_db.CategorySet.Update(obj);
_db.SaveChanges();
TempData["success"] = "Category updated successfully";
return RedirectToAction("Index");
}
return View(obj);
}
View
Here's an example of a "Create Category" page for an app:
@model Category
<h1>Create</h1>
<form method="post">
<div class="border p-3 mt-4">
<h2 class="text-primary">Create Category</h2>
<div asp-validation-summary="All"></div>
<div class="row pb-2">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span class="text-danger">@Html.ValidationMessageFor(m => m.Name)</span>
</div>
<div class="row pb-2">
<label asp-for="DisplayOrder"></label>
<input asp-for="DisplayOrder" class="form-control" />
<span class="text-danger">@Html.ValidationMessageFor(m => m.DisplayOrder)</span>
</div>
<button type="submit" class="btn btn-primary" style="width: 150px;">Create</button>
<a asp-controller="Category" asp-action="Index" class="btn btn-secondary" style="width: 150px;">
Back To List
</a>
</div>
</form>
@section Scripts{
@{
<partial name="_ValidationScriptsPartial" />
}
}
At the top, we're bringing in my Category model with @model Category
. The controller takes care of sending this to the view both on GET and POST requests:
For the most part, I'm just writing regular html. If I needed to include anything from the model, I could throw @Category.Name
anywhere and it will render to the page. Any C# that I wanted to write just requires the @
symbol.
Tag Helpers
The killer part of the example for me are the asp-
attributes. These are Tag Helpers that inject a lot of functionality automatically.
Take a look at the name field:
<div class="row pb-2">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span class="text-danger">@Html.ValidationMessageFor(m => m.Name)</span>
</div>
asp-for
on the label and input will know to populate the name input with the value from name, as well as pull from it when the form is submitted. Also included is client side validation automatically. When an error is found, the message is then passed to the @Html.ValidationMessageFor(m => m.Name)
That's it! It's a fair bit of magic, but it saves a lot of code that would normally be done by hand in JavaScript, or requiring a heavy library.